在PHP中使用Skyscanner Api的航班价格获取错误 [英] Get error in using skyscanner Api of flight pricing in php

查看:73
本文介绍了在PHP中使用Skyscanner Api的航班价格获取错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在点击链接以获取航班实时价格 所有机场位置的Skyscanner API和使用的Travel API CURL

 i have replace api key with my apikey. 

PHP代码:

    <?php

    $varApiKey = '?apiKey=API KEY';
    $country_code = 'IR';
    $originplace = '51.845159,-8.492835-latlong';
    $curency = 'EUR';
    $destination = 'DUB-iata';
    $start_date = date('Y-m-d');
    $dateOneMonth = strtotime($start_date);
//$end_date = date("Y-m-d", strtotime("+1 month", $dateOneMonth));
    $end_date = '';
    $audult = '1';
    $cabinclass = 'Economy';
    $locationschema = 'iata';
    $grouppricing = $preferDirects = 'true';

    $query = "&country=" . $country_code;
    $query .= "&currency=" . $curency;
    $query .= "&locale=en-IE";
    $query .= "&originplace=" . $originplace;
    $query .= "&destinationplace=" . $destination;
    $query .= "&inbounddate=" . $end_date;
    $query .= "&outbounddate=" . $start_date;
    $query .= "&adults=" . $audult;
    $query .="&locationschema=" . $locationschema;
    $query .="&cabinclass=" . $cabinclass;
    $query .="&preferDirects=" . $preferDirects;
    $query .="&grouppricing=" . $grouppricing;


    $apiParamsUrl = "http://partners.api.skyscanner.net/apiservices/pricing/v1.0/" . $varApiKey . $query . "";

    $apiParamsStr = parse_url($apiParamsUrl, PHP_URL_QUERY); // get the query string parametures
    parse_str($apiParamsStr, $apiParamsArray); 


    // parse into an array
// the api url. First we need to request for a session
    $apiSessionUrl = 'http://partners.api.skyscanner.net/apiservices/pricing/v1.0';

//open connection
    $ch = curl_init();

//set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_URL, $apiSessionUrl);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded', 'Accept: application/json')); // make api return json data
    curl_setopt($ch, CURLOPT_POST, count($apiParamsArray)); // set how many fiels
    curl_setopt($ch, CURLOPT_POSTFIELDS, $apiParamsStr);    // set the fields
// caputre the headers
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);

//execute post
    $response = curl_exec($ch);



// get the headers
    $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    $header = substr($response, 0, $header_size);
    $body = substr($response, $header_size);

//close connection
    curl_close($ch);

//    print_r($response);
//    die();
// get the api session url
    preg_match('~Location: ([^\s]+)~', $header, $matches);
    $apiSessionUrl = $matches[1];

// add on the api key for the session
    $apiSessionUrl .= $varApiKey;

// get the json data
    $data = file_get_contents($apiSessionUrl);

// decode the json
    $array = json_decode($data, true);

// dump json array`enter code here`
    printf('<pre>Poll Data  %s</pre>', print_r($array, true));


?>

运行代码后,我收到此错误

Notice: Undefined offset: 1 in C:\xampp\htdocs\flyoften\price.php on line 64

Warning: file_get_contents(?apiKey=in642116937922782575755373514454): failed to open stream: No such file or directory in C:\xampp\htdocs\flyoften\price.php on line 70
Poll Data  

如何解决此问题.友善的帮助 1:Skyscanner航班实时价格api是否已禁用?

解决方案

在这里preg_match没有任何结果,没有$matches[1]:

preg_match('~Location: ([^\s]+)~', $header, $matches);
$apiSessionUrl = $matches[1];

您应该测试preg_match的结果:

if (preg_match('~Location: ([^\s]+)~', $header, $matches)) {
    $apiSessionUrl = $matches[1];
} else {
    // There is a problem !!!
    // do something ... or exit the program
}

i am following link for accessing flight live prices Skyscanner API for all airport locations and Travel API using CURL

 i have replace api key with my apikey. 

PHP Code:

    <?php

    $varApiKey = '?apiKey=API KEY';
    $country_code = 'IR';
    $originplace = '51.845159,-8.492835-latlong';
    $curency = 'EUR';
    $destination = 'DUB-iata';
    $start_date = date('Y-m-d');
    $dateOneMonth = strtotime($start_date);
//$end_date = date("Y-m-d", strtotime("+1 month", $dateOneMonth));
    $end_date = '';
    $audult = '1';
    $cabinclass = 'Economy';
    $locationschema = 'iata';
    $grouppricing = $preferDirects = 'true';

    $query = "&country=" . $country_code;
    $query .= "&currency=" . $curency;
    $query .= "&locale=en-IE";
    $query .= "&originplace=" . $originplace;
    $query .= "&destinationplace=" . $destination;
    $query .= "&inbounddate=" . $end_date;
    $query .= "&outbounddate=" . $start_date;
    $query .= "&adults=" . $audult;
    $query .="&locationschema=" . $locationschema;
    $query .="&cabinclass=" . $cabinclass;
    $query .="&preferDirects=" . $preferDirects;
    $query .="&grouppricing=" . $grouppricing;


    $apiParamsUrl = "http://partners.api.skyscanner.net/apiservices/pricing/v1.0/" . $varApiKey . $query . "";

    $apiParamsStr = parse_url($apiParamsUrl, PHP_URL_QUERY); // get the query string parametures
    parse_str($apiParamsStr, $apiParamsArray); 


    // parse into an array
// the api url. First we need to request for a session
    $apiSessionUrl = 'http://partners.api.skyscanner.net/apiservices/pricing/v1.0';

//open connection
    $ch = curl_init();

//set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_URL, $apiSessionUrl);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded', 'Accept: application/json')); // make api return json data
    curl_setopt($ch, CURLOPT_POST, count($apiParamsArray)); // set how many fiels
    curl_setopt($ch, CURLOPT_POSTFIELDS, $apiParamsStr);    // set the fields
// caputre the headers
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);

//execute post
    $response = curl_exec($ch);



// get the headers
    $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    $header = substr($response, 0, $header_size);
    $body = substr($response, $header_size);

//close connection
    curl_close($ch);

//    print_r($response);
//    die();
// get the api session url
    preg_match('~Location: ([^\s]+)~', $header, $matches);
    $apiSessionUrl = $matches[1];

// add on the api key for the session
    $apiSessionUrl .= $varApiKey;

// get the json data
    $data = file_get_contents($apiSessionUrl);

// decode the json
    $array = json_decode($data, true);

// dump json array`enter code here`
    printf('<pre>Poll Data  %s</pre>', print_r($array, true));


?>

after runing the code i am getting this error

Notice: Undefined offset: 1 in C:\xampp\htdocs\flyoften\price.php on line 64

Warning: file_get_contents(?apiKey=in642116937922782575755373514454): failed to open stream: No such file or directory in C:\xampp\htdocs\flyoften\price.php on line 70
Poll Data  

how to solve this issue . kindly help 1:is skyscanner flight live prices api disable?

解决方案

The preg_match doesn't give any result here, there is no $matches[1]:

preg_match('~Location: ([^\s]+)~', $header, $matches);
$apiSessionUrl = $matches[1];

You should test the result of preg_match:

if (preg_match('~Location: ([^\s]+)~', $header, $matches)) {
    $apiSessionUrl = $matches[1];
} else {
    // There is a problem !!!
    // do something ... or exit the program
}

这篇关于在PHP中使用Skyscanner Api的航班价格获取错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆