0

I want to extract data from this URL with Php Curl

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

But i got an empty page.

I check to that link and inspect with mozilla network->xhr and there is an content in response tab.

How I can extract the data from that link with php curl?

Here is my code

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

I must add 'X-Requested-With: XMLHttpRequest' at header of that link then response will come out.

edit header

Then the response will come out [response result][2]

3
  • 2
    That page also returns an empty response for me - are you sure it doesn't require cookies to be set? The /ajax/ at the start of the path suggests that it might require a session to already exist. Commented Aug 15, 2017 at 16:08
  • Nothing in response.. Commented Aug 15, 2017 at 16:08
  • I'm forget this one. Add 'X-Requested-With: XMLHttpRequest' at the header of that link, then response will comeout Commented Aug 15, 2017 at 16:13

2 Answers 2

1

You've done a mistake with CURLOPT_HTTPHEADER. You set params as Key => Value but it's incorrect. You should set params as values with ":" as the delimiter.

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

Also I have removed this string to get uncompressed data

"Accept-Encoding: gzip, deflate, br",
Sign up to request clarification or add additional context in comments.

1 Comment

and next time, check the return value of curl_setopt! if you did that, it would've told you already that the setopt failed, and if further had used curl_error() to extract the error, it'd tell you that that the format was invalid. use something like function ecurl_setopt ($ch , int $option , $value ){ $ret=curl_setopt($ch,$option,$value); if($ret!==true){ throw new RuntimeException ( 'curl_setopt() failed. curl_errno: ' . return_var_dump ( curl_errno ($ch) ).'. curl_error: '.curl_error($ch) ); } }
0

Mate are u sure that this link work? because I inspected the code with mozilla and chrome and the response is empty, and I tried to scrape with a simple php class that i have with curl and the result is empty cuz the link dont have response

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.