本文介绍了PHP Curl返回200,但不发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉打扰您,但我无法弄清楚代码中的错误是什么.我正在尝试对以下内容发出HTTP发布请求,我的响应代码为200,但不起作用.下面是我的代码.如果有人能帮助我,我将非常感谢他的努力.

I'm sorry to disturb you but I can't figure out what is the error on my code. I'm trying to Make a HTTP Post Request on the following , my response code is 200 but It doesn't work.Below is my code. If anyone can help I would appreciate his effort very much.

    <?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;
?>

有关我收到的错误的curl信息在以下代码上.

The curl informations about errors that I receive are on the following code.

非常感谢您的帮助!

推荐答案

要使发布字符串正确,您必须对其进行urlencode.例如,在描述字符串中,您可以使用空格来分隔发布字符串.

To make the post string correct, you must urlencode it. For example in description string you have spaces which break the post string.

更改

$post_items[] = $key . '=' . $value;

$post_items[] = $key . '=' . urlencode($value);

这篇关于PHP Curl返回200,但不发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 18:29