0

I made a PHP script to send HTTP requests on a website. Everything works "fine" when I don't set the header. The result is wrong though. But the program freezes when I change the headers. Here's my code:

$cookie_file_path='cookie';

$curl_connection =  curl_init('http://www.website.com/path/to/script');

curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);

curl_setopt($curl_connection, CURLOPT_USERAGENT,"Mozilla/5.0 (X11; Linux x86_64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1");

curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);

curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($curl_connection, CURLOPT_COOKIEJAR, $cookie_file_path);

curl_setopt($curl_connection, CURLOPT_COOKIEFILE, $cookie_file_path);

curl_setopt($curl_connection,CURLOPT_HTTPHEADER, array('Accept: application/json, text/javascript, */*; q=0.01','Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3','Accept-Encoding: gzip, deflate','Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7','Content-Type: application/json; charset=utf-8','X-Requested-With: XMLHttpRequest','Content-Length: 179','Pragma: no-cache','Cache-Control: no-cache'));

//I use the same header as the website

$post_data=array();

$post_data['address'] = 'my_address';

$post_data['lastname'] = 'my_lastname';

 (...)

foreach ( $post_data as $key => $value)
    $post_items[] = $key . '=' . $value;


$post_string = implode ('&', $post_items);

curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

$result = curl_exec($curl_connection);

file_put_contents('result.html',$result);

The main web page sends a POST request in AJAX to a script, and I'm trying to do the same with PHP. The script doesn't freeze when I comment out this line :

curl_setopt($curl_connection,CURLOPT_HTTPHEADER, array('Accept: application/json, text/javascript, */*; q=0.01','Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3','Accept-Encoding: gzip, deflate','Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7','Content-Type: application/json; charset=utf-8','X-Requested-With: XMLHttpRequest','Content-Length: 179','Pragma: no-cache','Cache-Control: no-cache'));

What are the possible reasons of this. Am I missing something?

Thanks

user1319182
  • 481
  • 12
  • 26

1 Answers1

3

One likely reason is that you're sending the static string;

Content-Length: 179

...as a header. If your content isn't actually that long, the web server may be waiting for data you'll never send before replying to your request.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • Thanks for your reply. Then what is the best way to set this string dynamically? I get an error when I remove it : "There was an error processing the request.","StackTrace":"","ExceptionType":""}" – user1319182 Oct 04 '12 at 21:56
  • I fixed the issue with this post : http://stackoverflow.com/questions/4271621/php-curl-post-json. The array must be encoded in Json: curl_setopt($curl_connection, CURLOPT_POSTFIELDS, json_encode($post_data)); – user1319182 Oct 04 '12 at 22:20