0

我制作了一个 PHP 脚本来在网站上发送 HTTP 请求。当我不设置标题时,一切都“正常”。结果是错误的。但是当我更改标题时程序会冻结。这是我的代码:

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

主网页将 AJAX 中的 POST 请求发送到脚本,我正在尝试对 PHP 做同样的事情。当我注释掉这一行时,脚本不会冻结:

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

这可能是什么原因。我错过了什么吗?

谢谢

4

1 回答 1

3

一个可能的原因是您正在发送静态字符串;

Content-Length: 179

...作为标题。如果您的内容实际上没有那么长,则 Web 服务器可能会在回复您的请求之前等待您永远不会发送的数据。

于 2012-10-04T20:46:44.967 回答