我正在尝试使用以下代码发送一个POST页面:
$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
对不起我的英文..
答案 0 :(得分:1)
无需发出自定义请求,发送此类标头或使用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);
答案 1 :(得分: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);
答案 2 :(得分:0)
问题是,这个表单不是multipart / form-data,然后你将创建一个查询字符串来发布。请参阅联机帮助页http://php.net/manual/en/function.curl-setopt.php。然后给出一个数组。标题将设置multipart / form-data。这是无效的请求。
解决方案
字符串很容易构建“& itemid =”。json ['id']。“& sh =”。json ['sh']; 把它放在CURLOPT_POSTFIELDS中。
saludos(再见)抱歉我的英语......