无法使用PHP curl发布数据

时间:2013-11-29 06:18:21

标签: php curl

我正在尝试使用以下代码将一些数据发布到使用cURL的Web服务:

$response = "<p>Here is your RMA information. Please ship the product to the address below.<br>
&nbsp;<br>
IMPORTANT:<br>
1) Refunds can only be issues for purchases made within 30 days. Products missing any accessories or original packaging will require a 20% restocking fee.<br>Please see our RMA Guidelines here:<br>
We will update you with the progress of the RMA once we receive your RMA. We appreciate your patience and understanding.<br>
</p>";

$url = "https://mysite.desk.com/api/v2/cases/18/notes";            
$username = "my username";
$password = "my password";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"body":"' . $response . '"}');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$output = curl_exec($ch);

它给了我以下错误

[url] => https://mysite.desk.com/api/v2/cases/18/notes
[content_type] => 
[http_code] => 500
[header_size] => 165
[request_size] => 228
[filetime] => -1
[ssl_verify_result] => 20
[redirect_count] => 0
[total_time] => 1.373
[namelookup_time] => 0
[connect_time] => 0.281
[pretransfer_time] => 0.811
[size_upload] => 2235
[size_download] => 0
[speed_download] => 0
[speed_upload] => 1627
[download_content_length] => -1
[upload_content_length] => 2235
[starttransfer_time] => 1.092
[redirect_time] => 0

我不明白确切的问题。请帮帮我。

2 个答案:

答案 0 :(得分:0)

如果json格式数据看起来很复杂,请尝试数组

$ post_array =阵列( '体'=&GT; $响应);

curl_setopt($ ch,CURLOPT_POSTFIELDS,$ post_array;

答案 1 :(得分:0)

通过更改以下行来解决

$data = array();
$data['body'] = preg_replace("/&#?[a-z0-9]{2,8};/i", "", strip_tags($_POST['response']));

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));