1

I am getting 400 http_code error when I send a Ajax request with CURL below is my 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);

// I am 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] => 
)
//// 
Smern
  • 18,746
  • 21
  • 72
  • 90
Fiaz Ahmad
  • 55
  • 1
  • 9
  • 1
    `400` means it's a bad request. Figure out what is bad. Maybe a bad header? – Halcyon May 15 '15 at 14:21
  • Yes you are right it's client side error but if I not set header it gives 500 http_code and I have also tried many combination for making header ... :) – Fiaz Ahmad May 15 '15 at 14:24
  • Could you narrow down your problem to a minimal example? – cmbarbu May 15 '15 at 14:33
  • @cmbarbu now I have narrow my code . Please have a look – Fiaz Ahmad May 15 '15 at 14:58
  • What is your $data_url ? And i think Host and Referer should be different. Host is the domain you're curling : domain.com – tetram May 15 '15 at 15:05
  • @tetram $data_url is POST array and I am using the right host and refer but actually it's confidential so I can't write here..... – Fiaz Ahmad May 15 '15 at 15:10
  • @Halcyon yeah that's really all that can be said here, until Fiaz either shows a Fiddler Proxy log of how the real browser-generated request looks like, or shows us the actual url so we can try ourselves. i recommend everyone vote-to-close this question as "can not reproduce" until more information is provided, because we really can not reproduce this atm. – hanshenrik Jul 15 '19 at 08:52

1 Answers1

0

I am not sure it would help you but i am able to successfully query SSL site with the following code. There are several things like user agent, cookiefile, SSL verify, url form encoded for post data and subsequent request should use the same cookie and 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 it is using the 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);
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
magegaga.com
  • 500
  • 3
  • 7