【问题标题】:PHP cURL and response code 302PHP cURL 和响应代码 302
【发布时间】:2013-04-06 16:11:49
【问题描述】:

我正在尝试使用以下代码发送一个页面:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.olx.es/posting_success.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1); 
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTREDIR, 2);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($ch, CURLOPT_POSTFIELDS, array("itemid" => $json['id'], "sh" => $json['sh']));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded"));
$response = curl_exec($ch);
curl_close($ch);

在这样做的过程中,我返回了一个服务器错误,提示“无效请求”。

我注意到,如果我通过 telnet 手动执行请求,我第一次返回 302,尽管我已经关注他,但不起作用,也许是这样。

wireshark 获得了客户实际发送的内容,我做错了什么?

**Hypertext Transfer Protocol:**
POST /posting_success.php HTTP/1.1
Host: www.olx.es
Connection: keep-alive
Content-Length: 52
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Origin: http://www.olx.es
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31
Content-Type: application/x-www-form-urlencoded
Referer: http://www.olx.es/posting.php?categ_id=322
Accept-Encoding: gzip,deflate,sdch
Accept-Language: es-ES,es;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

**Line-based text data: application/x-www-form-urlencoded:**
itemid=501347053&sh=b9302ed20769ae3717f896a33a369aa2

对不起我的英语..

【问题讨论】:

  • 可以分享原版页面吗?
  • 也许不是因为这个,但是如果你使用数组作为CURLOPT_POSTFIELDS的值,那么你应该使用不同的Content-type,来自PHP文档:如果值是一个数组,Content-Type 标头将设置为 multipart/form-data。

标签: php curl http-status-code-302


【解决方案1】:

您的 POST 请求似乎有问题。请尝试

$fields=array('itemid' => $json['id'], 'sh' =>urlencode ($json['sh']));

格式化您的字段

foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');

然后在 Curl OPt 中使用

    curl_setopt($ch1,CURLOPT_POST,count($fields));
    curl_setopt($ch1,CURLOPT_POSTFIELDS,$fields_string);

【讨论】:

  • 你好,我按照你的要求做了,没办法,返回了如下 Bad Request

    Bad Request

    您的浏览器发送了一个此服务器无法理解的请求。

    参考 #7.77db6ad4.1365972392.33761686 ** vardump ** [fields] => 数组([itemid] => 501855068 [sh] => fb9c6e45c0a2dbd3a497029425f2e4b6)

  • 我用wireshark查看了我的请求,现在我返回错误411 Length Required
  • 您还缺少一些东西。尝试在标题中设置Content-length。通常它代表响应服务器的故障
  • 您好,我已经添加了 Content-length 所以: curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Length: ".strlen($fields_string), "Content-type: application/x -www-form-urlencoded"); 但现在在 curl 调用中,服务器似乎没有响应,甚至没有跳过 php 超时或任何东西..
【解决方案2】:

无需进行自定义请求、发送此类标头或使用 CURLINFO_HEADER_OUT。
试试这个:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.olx.es/posting_success.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTREDIR, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query( array("itemid" => $json['id'], "sh" => $json['sh']) ));
$response = curl_exec($ch);
curl_close($ch);

【讨论】:

    【解决方案3】:

    问题是,这个表单不是multipart/form-data,然后你会创建一个查询字符串来发布。参见手册页http://php.net/manual/en/function.curl-setopt.php。然后你给一个数组。标头将设置为 multipart/form-data。这是无效的请求。

    解决方案是

    字符串很容易构建 "&itemid=".json['id']."&sh=".json['sh']; 把它放在 CURLOPT_POSTFIELDS 中。

    saludos(再见),对不起我的英语......

    【讨论】:

      猜你喜欢
      • 2021-05-22
      • 1970-01-01
      • 2012-03-02
      • 1970-01-01
      • 2013-09-21
      • 2021-08-09
      • 2017-12-29
      • 2012-04-01
      • 2021-10-27
      相关资源
      最近更新 更多