使用PHP Curl清空结果,但响应以json格式存在

时间:2017-08-15 16:04:59

标签: php xml curl

我想用Php Curl从这个URL中提取数据

https://www.tiket.com/ajax/pingFlightSearch?d=CGK&a=DPS&date=2017-08-18&adult=1&child=0&infant=0&airlines=%5B%22LION%22%5D&subsidy=true&page_view=roundseperate

但我得到一个空白页面。

我查看该链接并使用mozilla network-> xhr进行检查,并在响应标签中显示内容。

如何使用php curl从该链接中提取数据?

这是我的代码

$url = 'https://www.tiket.com/ajax/pingFlightSearch?d=CGK&a=DPS&date=2017-08-18&adult=1&child=0&infant=0&airlines=%5B%22LION%22%5D&subsidy=true&page_view=roundseperate';

$cURL = curl_init();

curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17');   
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($cURL, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($cURL, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($cURL, CURLOPT_AUTOREFERER, true); 
curl_setopt($cURL, CURLINFO_HEADER_OUT, true);
curl_setopt($cURL, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($cURL, CURLOPT_VERBOSE,true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
    "Host" => "www.tiket.com",
    "User-Agent" => "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0",
    "Accept" => "application/json, text/javascript, */*; q=0.01",
    "Accept-Language" => "en-us,en;q=0.5",
    "Accept-Encoding" => "gzip, deflate, br",    
    "Content-Type" => "application/cap+xml;charset=utf-8",
    "X-NewRelic-ID" => "UQIGUlJXGwACUFZaAAM=",
    "X-Requested-With" => "XMLHttpRequest",         
    "Connection" => "keep-alive",
    "Pragma" => "no-cache",
    "Cache-Control" => "no-cache"
)); 

$result = curl_exec($cURL);

print_r($result);

curl_close($cURL);

我必须在该链接的标题处添加“X-Requested-With:XMLHttpRequest”,然后响应将会出现。

edit header

然后响应将会出来 [回应结果] [2]

2 个答案:

答案 0 :(得分:1)

你在使用CURLOPT_HTTPHEADER时犯了一个错误。你将params设为Key =>价值但不正确。您应该使用":"将参数设置为值。作为分隔符。

curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
    "Host: www.tiket.com",
    "Accept: application/json, text/javascript, */*; q=0.01",
    "Accept-Language: en-us,en;q=0.5",
    "Content-Type: application/cap+xml;charset=utf-8",
    "X-NewRelic-ID: UQIGUlJXGwACUFZaAAM=",
    "X-Requested-With: XMLHttpRequest",         
    "Connection: keep-alive",
    "Pragma: no-cache",
    "Cache-Control: no-cache"
));

此外,我已删除此字符串以获取未压缩的数据

"Accept-Encoding: gzip, deflate, br",

答案 1 :(得分:0)

Mate你确定这个链接有用吗?因为我用mozilla和chrome检查了代码并且响应是空的,我试图用一个简单的php类抓住我用卷曲,结果是空的,因为链接没有响应