简体   繁体   中英

PHP curl doesn't return all the response

I am trying to use an API in php which (as usual) returns a large json response with the requested data.

It works like this:

1) I have to make a post request with the data i want (in a certain format which is not important here) and then,

2) Visit another url where with a get parameter which i get from the response of step one to get the actual data.

The problem is that i tryed both curl() and get_file_contents() to obtain the json data but it seems that i do not get the whole json. I visit the url from my browser and get the json manually and compare and see if the two match.

What can possibly be wrong?

Here is my code:

$tp = new TravelPayoutApi();
$req = $tp->request_one_way('en', 'Y', 1,0,0, 'BER', 'PAR', '2017-10-22');
//$req = $tp->requestRoundTrip('en', 'Y', 1,0,0, 'BER', 'PAR', '2017-10-22','2017-10-25');
echo "req =" . $req;

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://api.travelpayouts.com/v1/flight_search"); // $apiURL = "http://api.travelpayouts.com/v1/flight_search";

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));                                    

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// $response = array();
// echo $response;
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response, true);
$search_id = $response['search_id'];
echo $search_id;

This was the part that gets the parameter i need to collect the data

Here are my efforts to obtain and echo them:

$results = file_get_contents("http://api.travelpayouts.com/v1/flight_search_results?uuid=" . $search_id);
var_dump($results); // to see if this is really json
// $ch1 = curl_init();
// $resultsURL = "http://api.travelpayouts.com/v1/flight_search_results?uuid=" . $search_id;
// echo "This is the results URL:" . $resultsURL;
// curl_setopt($ch1, CURLOPT_URL, $resultsURL);
// curl_setopt($ch1, CURLOPT_RETURNTRANSFER,1);

// $results = curl_exec($ch1);
// $results = json_encode($results);
// curl_close($ch1);

The commented lines at the and are different scripts i have tried. Take a look at them too.

EDIT:

Ok, i tried the original curl command the API (curl -v -H 'Accept-Encoding:gzip,deflate,sdch' htttp://api.travelpayouts.com/v1/flight_search_results?uuid=my_id_here --compressed) , and it works like a charm. But it seems that i can not get it to work with php curl. I always get my results truncated. What can i do about this?

My current code to get the results is:

$ch1 = curl_init();

curl_setopt($ch1, CURLOPT_URL, $resultsURL);
curl_setopt($ch1, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip,deflate,sdch')); 
curl_setopt($ch1, CURLOPT_ENCODING, 'gzip,deflate,sdch');
// curl_setopt($ch1, CURLOPT_CONNECTTIMEOUT, 0);
//curl_setopt($ch1, CURLOPT_TIMEOUT, 15); // number of seconds to allow curl to execute

$results = curl_exec($ch1);
echo($results);
curl_close($ch1);

Since you get data back try the following to check if all the data you want will be inserted to the file.

$results = file_get_contents("http://api.travelpayouts.com/v1/flight_search_results?uuid=" . $search_id);
    $fp = fopen($path_to_file, "r");  
    $fileLines = array();
    while (!feof($fp)){
      array_push(fgets($fp),$results);
    } 
    fclose($$fp);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM