400 Bad Request - PHP Curl JSON请求

时间:2014-12-03 10:16:35

标签: php json curl

当我尝试向php中的服务器发送请求时,我收到400 Bad Request。

使用此API -

https://sandbox.dropship.provet.com.au/orders

"创建订单"调用

代码是 -

<?php //set POST variables $username='user'; $password='pass'; $url = 'https://sandbox.dropship.provet.com.au/api/orders'; $fields = array( 'CustomerName' => 'Customer Name', 'AddressLine1' => 'Customer Name Address', 'Suburb' => 'Albany', 'State' => 'Perth', 'Postcode' => '6330', 'Country' => 'Australia', 'CustomerEmail' => 'customername@gmail.com', 'CustomerPhone' => '9425361366', 'OrderLines' => array('ProductCode' => 'ACTI SO 10', 'Quantity'=>1), ); $data_string = json_encode($fields); echo strlen($data_string); exit(); $ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string)) ); curl_setopt($ch, CURLOPT_TIMEOUT, 15); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15); curl_setopt($ch,CURLOPT_HTTPAUTH, CURLAUTH_ANY); curl_setopt($ch,CURLOPT_USERPWD, "$username:$password"); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER , FALSE); $result = curl_exec($ch); if ($result === false) { // throw new Exception('Curl error: ' . curl_error($crl)); print_r('Curl error: ' . curl_error($ch)); exit(); } //close connection curl_close($ch); print_r(json_decode($result)); ?> </pre>

我该如何解决这个问题?

0 个答案:

没有答案