【问题标题】:PHP curl doesn't return all the responsePHP curl 不返回所有响应
【发布时间】:2017-10-06 08:12:35
【问题描述】:

我正在尝试在 php 中使用 API,它(像往常一样)返回带有请求数据的大型 json 响应。

它的工作原理是这样的:

1) 我必须使用我想要的数据(以某种在这里不重要的特定格式)发出发布请求,然后,

2) 使用我从第一步的响应中获取的获取参数访问另一个 url,以获取实际数据。

问题是我尝试了 curl() 和 get_file_contents() 来获取 json 数据,但似乎我没有得到整个 json。我从浏览器访问 url 并手动获取 json 并比较两者是否匹配。

可能有什么问题?

这是我的代码:

$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;

这是获取我需要收集数据的参数的部分

以下是我为获取和回应它们所做的努力:

$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);

和 的注释行是我尝试过的不同脚本。也看看他们。

编辑:

好的,我尝试了 API 的原始 curl 命令 (curl -v -H 'Accept-Encoding:gzip,deflate,sdch' htttp://api.travelpayouts.com/v1/flight_search_results?uuid=my_id_here --compressed),它就像一个魅力。但似乎我无法让它与 php curl 一起使用。我的结果总是被截断。我该怎么办?

我目前获取结果的代码是:

$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);

【问题讨论】:

  • 检查错误日志,看看连接是否超时,或者你没有达到php的内存限制。
  • CURLOPT_POSTFIELDS 用于POST 请求。 GET 请求参数应该是CURLOPT_URL 字符串的一部分。
  • @azurinko 你指的是哪个错误日志?
  • @AlexBlex 这是我发送的第一个获取 search_id 参数的请求,所以我用它来检索结果。请阅读全文。我正在解释它是如何工作的
  • 啊,对不起,我的错。看来这两个请求都是有效的。我会使用 tcpdump 或 wireshark 来检查 TCP 级别的情况。你是在 *nix 还是 windows 上?

标签: php json curl truncated


【解决方案1】:

既然您获得了数据,请尝试以下操作以检查您想要的所有数据是否都将插入到文件中。

$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);

【讨论】:

  • 通过上面的代码示例,您将确定它是来自您调用的 URL 的帖子问题(例如帖子最大大小)还是视图限制,因为它是一个很大的 json,如您所说。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多