سؤال

I have 0 experince with curl, and i want to run a request that will return a json body inside php.

using firefox firbug i can copy curl order and it looks like this

curl 'http://*****.com/index.php/home/search/keyword/c815f2fec5?value=keyworkd' -H 
'Host: *****.com' -H 
'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0' -H
'Accept: */*' -H 
'Accept-Language: en-US,en;q=0.5' -H 
'Accept-Encoding: gzip, deflate' -H
'X-Requested-With: XMLHttpRequest' -H 
'Referer: http://****.com/' -H
'Cookie: _ga=GA1.2.373422434.1399222050; PHPSESSID=l8sf036kjaijt6cjvcqnu992l4'

can i simulate such request with php ?

هل كانت مفيدة؟

المحلول

Of course it is, without going much into unnecessary things and based on your limited info:

<?php
    $c = curl_init('http://*****.com/index.php/home/search/keyword/c815f2fec5?value=keyworkd');                 
    curl_setopt($c, CURLOPT_USERAGENT, "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0");
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);         
    curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);     
    curl_setopt($c, CURLOPT_COOKIE, 
    '_ga=GA1.2.373422434.1399222050; PHPSESSID=l8sf036kjaijt6cjvcqnu992l4');
    curl_setopt($c, CURLOPT_REFERER, 'http://****.com/');
    $z = curl_getinfo($c);
    $s = curl_exec($c);
    curl_close($c);  
?>

Edit: Damn though it is POST not COOKIE, repaired. Edit2: With cookie file.

<?php
    $cookie_file = "cookie.txt"; //remember to check if it exists
    $c = curl_init('http://*****.com/index.php/home/search/keyword/c815f2fec5?value=keyworkd');                 
    curl_setopt($c, CURLOPT_USERAGENT, "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0");
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);         
    curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);     
    curl_setopt($c, CURLOPT_COOKIEJAR, $cookie_file);
    curl_setopt($c, CURLOPT_COOKIEFILE, $cookie_file);
    curl_setopt($c, CURLOPT_REFERER, 'http://****.com/');
    $z = curl_getinfo($c);
    $s = curl_exec($c);
    curl_close($c);  
?>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top