很抱歉打扰你,但我无法弄清楚代码上的错误是什么。我正在尝试对以下内容发出HTTP Post请求,我的响应代码是200,但它不起作用。 以下是我的代码。如果有人可以提供帮助,我会非常感谢他的努力。
<?php
//create array of data to be posted
$post_data['data[title]'] = 'TITLE';
$post_data['data[category_id]'] = 'Register';
$post_data['data[private_business]'] = 'private';
$post_data['data[description]'] = 'Description for anyone';
$post_data['data[param_size]'] = '1129';
$post_data['data[param_price][currency]'] = 'RON';
//$post_data['data[param_price][0]'] = 'price';
$post_data['data[param_price][1]'] = '100';
//$post_data['data[param_price][2]'] = '';
$post_data['data[param_state]'] = 'used';
$post_data['data[city]'] = '26711';
$post_data['data[person]'] = 'Ion';
$post_data['data[email]'] = 'email';
$post_data['data[accept]'] = 'on';
//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);
//create cURL connection
$curl_connection =
curl_init('http://olx.ro/i2/adauga-anunt/');
//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,
"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.17 Safari/537.36");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_HEADER, 0);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
$headers = array();
//$headers[] = 'Host: olx.ro';
//$headers[] = 'User-Agent:"Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:35.0) Gecko/20100101 Firefox/35.0';
//$headers[] = 'Accept: */* ';
//$headers[] = 'Accept-Language: en-US,en;q=0.5';
//$headers[] = 'Accept-Encoding: gzip, deflate';
//$headers[] = 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8';
//$headers[] = 'X-Requested-With: XMLHttpRequest';
//$headers[] = 'Referer: http://olx.ro/i2/adauga-anunt/';
//$headers[] = 'Content-Length: 594';
//$headers[] = 'Cookie: PHPSESSID=otsg9lalsh5m06faaviuqtmih4; xtvrn=$542850$540516$; optimizelySegments=%7B%221067457090%22%3A%22ff%22%2C%221087063106%22%3A%22none%22%2C%221060248541%22%3A%22direct%22%2C%221042136847%22%3A%22false%22%2C%221092431561%22%3A%22none%22%2C%221079661599%22%3A%22ff%22%2C%221027596252%22%3A%22false%22%2C%221069071887%22%3A%22direct%22%7D; optimizelyEndUserId=oeu1425804897718r0.9477511105813824; optimizelyBuckets=%7B%7D; POPUPCHECK=1425891298025; is_tablet=0; __utma=29471969.1470095060.1425804898.1425804898.1425804898.1; __utmb=29471969.61.10.1425804898; __utmc=29471969; __utmz=29471969.1425804898.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); last_used_locations=%5B%7B%22regionId%22%3A%224%22%2C%22cityId%22%3A%2226711%22%2C%22districtId%22%3Anull%2C%22label%22%3A%22Brasov%22%2C%22details%22%3A%22Brasov%22%2C%22source%22%3A%22adding%22%2C%22precision%22%3A%22precision-adding%22%7D%5D; mobile2=desktop; ki_t=1425808476633%3B1425808476633%3B1425808476633%3B1%3B1; ki_r=';
//$headers[] = 'Connection: keep-alive';
//$headers[] = 'Pragma: no-cache';
//$headers[] = 'Cache-Control: no-cache';
//curl_setopt($curl_connection, CURLOPT_HTTPHEADER, $headers);
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
//perform our request
$result = curl_exec($curl_connection);
//show information regarding the request
print_r(curl_getinfo($curl_connection));
echo curl_errno($curl_connection) . '-' .
curl_error($curl_connection);
if(curl_errno($c))
{
echo 'error:' . curl_error($curl_connection);
}
//close the connection
curl_close($curl_connection);
return $result;
?>
我收到的有关错误的卷曲信息在以下代码中。
数组([url] =&gt; http://olx.ro/i2/adauga-anunt/ [content_type] =&gt; 为text / html; charset = UTF-8 [http_code] =&gt; 200 [header_size] =&gt; 418 [request_size] =&gt; 556 [filetime] =&gt; -1 [ssl_verify_result] =&gt; 0 [redirect_count] =&gt; 0 [total_time] =&gt; 0.35955 [namelookup_time] =&gt; 0.009494 [connect_time] =&gt; 0.160634 [pretransfer_time] =&gt; 0.160793 [size_upload] =&gt; 302 [size_download] =&gt; 0 [speed_download] =&gt; 0 [speed_upload] =&gt; 839 [download_content_length] =&gt; 0 [upload_content_length] =&gt; 302 [starttransfer_time] =&gt; 0.359489 [redirect_time] =&gt; 0 [certinfo] =&gt; Array()[primary_ip] =&gt; 178.21.159.40 [primary_port] =&gt; 80 [local_ip] =&gt; 172.17.64.2 [local_port] =&gt; 36961 [redirect_url] =&gt; )0 -
非常感谢你的帮助!!
答案 0 :(得分:1)
要使帖子字符串正确,您必须对其进行urlencode。例如,在描述字符串中,您有空格来破坏帖子字符串。
更改
$post_items[] = $key . '=' . $value;
到
$post_items[] = $key . '=' . urlencode($value);