javascript - How to emulate Ajax request using PHP CURL? -
i getting 400 http_code error when send ajax request curl below code.
$header = array( "accept : application/json, text/javascript, */*; q=0.01", "accept-encoding : gzip, deflate", "accept-language : en-us,en;q=0.5", "content-type : application/json; charset=utf-8", "host : https://some.com", "referer : https://some.com/dashboard/reports", "x-requested-with : xmlhttprequest" ); $c = curl_init('https://domain.com/report.php'); //curl_setopt($c, curlopt_ssl_verifypeer, false); //curl_setopt($c, curlopt_verbose, true); curl_setopt($c, curlopt_useragent, $_server['http_user_agent']); curl_setopt($c, curlopt_returntransfer, 1); curl_setopt( $c, curlopt_post, true ); curl_setopt( $c, curlopt_postfields, $data_url ); curl_setopt($c, curlopt_httpheader, $header); //curl_setopt($ch, curlopt_httpheader, array("x-requested-with: xmlhttprequest")); curl_setopt($c, curlopt_cookiefile, $cookie_jar); $page = curl_exec($c); $httpcode = curl_getinfo($c);
// getting following response after making curl request
array ( [url] => some_url [content_type] => [http_code] => 400 [header_size] => 70 [request_size] => 935 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.205562 [namelookup_time] => 0.000132 [connect_time] => 0.032866 [pretransfer_time] => 0.170225 [size_upload] => 272 [size_download] => 0 [speed_download] => 0 [speed_upload] => 1323 [download_content_length] => 0 [upload_content_length] => 272 [starttransfer_time] => 0.205498 [redirect_time] => 0 [certinfo] => array ( ) [primary_ip] => 66.35.58.70 [primary_port] => 443 [local_ip] => 198.1.92.85 [local_port] => 53627 [redirect_url] => ) ////
i not sure able query ssl site following code. there several things user agent, cookiefile, ssl verify, url form encoded post data , subsequent request should use same cookie , agent data
$config['useragent'] = 'mozilla/5.0 (windows nt 6.2; wow64; rv:17.0) gecko/20100101 firefox/17.0'; $ch = curl_init(); curl_setopt($ch,curlopt_url,$url); curl_setopt($ch, curlopt_useragent, $config['useragent']); curl_setopt($ch, curlopt_referer, 'http://google.com/'); $dir = dirname(__file__); $config['cookie_file'] = $dir."/".md5($_server['remote_addr']) . '.txt'; curl_setopt($ch, curlopt_cookiefile, $config['cookie_file']); curl_setopt($ch, curlopt_cookiejar, $config['cookie_file']); curl_setopt($ch, curlopt_verbose, 1); curl_setopt($ch, curlopt_nobody, 0); curl_setopt($ch, curlopt_ssl_verifyhost, false); curl_setopt($ch, curlopt_ssl_verifypeer, false); curl_setopt($ch, curlopt_followlocation, true); curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch,curlopt_header,0); $html=curl_exec($ch);
subsequent call request(note using cookie, useragent generated in first request)
$post = 'city_id=3&length-3'; $headers = array(); $headers[] = 'accept: application/json, text/javascript, */*; q=0.01'; $headers[] = 'accept-language: en-us,en;q=0.5'; $headers[] = 'content-length:'.strlen($post); $headers[] = 'content-type: application/x-www-form-urlencoded; charset=utf-8'; $headers[] = 'x-requested-with: xmlhttprequest'; curl_setopt($ch, curlopt_httpheader, $headers); $ch = curl_init(); curl_setopt($ch,curlopt_url,$link); curl_setopt($ch, curlopt_useragent, $config['useragent']); curl_setopt($ch, curlopt_cookiefile, $config['cookie_file']); curl_setopt($ch, curlopt_cookiejar, $config['cookie_file']); curl_setopt($ch, curlopt_verbose, 1); curl_setopt($ch, curlopt_nobody, 0); curl_setopt($ch, curlopt_ssl_verifyhost, false); curl_setopt($ch, curlopt_ssl_verifypeer, false); curl_setopt($ch, curlopt_followlocation, true); curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch,curlopt_header,0); $linkhtml=curl_exec($ch);
Comments
Post a Comment