我們的開發人員在使用API(Brightpearl)時遇到了一些問題,並收到「嘗試將其解析爲請求正文時無法讀取JSON「消息時試圖運行下面的腳本。有沒有人有任何想法可能的問題?提前謝謝了。錯誤「嘗試將其解析爲JSON時,無法讀取請求正文」
<?php
define ("DB_HOST","localhost");
define ("DB_USER","busi6292_REMOVED");
define ("DB_PASSWORD","REMOVED");
define ("DB_DATABASE","busi6292_REMOVED");
$link = @mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)
or die ('I cannot connect to the database because: ' . @mysql_error());
@mysql_select_db (DB_DATABASE);
mysql_set_charset('utf8',$link);
// Brightperl Data Access Start
$authenticationDetails = array(
'apiAccountCredentials' => array(
'emailAddress' => 'REMOVED',
'password' => 'REMOVED',
),
);
$encodedAuthenticationDetails = json_encode($authenticationDetails);
$authenticationUrl = 'https://ws-eu1.brightpearl.com/REMOVED/authorise';
//$cresponse_token=getdata($url,$authenticationDetails);
$ch = curl_init();
$headers = array('Content-Type: application/json');
curl_setopt($ch, CURLOPT_URL, $authenticationUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($authenticationDetails));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (false === $response) {
echo 'Request unsuccessful' . PHP_EOL;
curl_close($ch);
exit(1);
}
$responseCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
$responseBody = json_decode($response);
curl_close($ch);
$authorisationToken = $responseBody->response;
// End
$sql = "select * from mail where InvoiceDate ='".date('Y-m-d')."'";
$query = mysql_query($sql);
while($sql_array = mysql_fetch_array($query))
{
//print_r($sql_array);
//echo $authorisationToken; exit;
// Brigtperl API Access for the Orders has been shipped Start
$ShippingGoodsoutURL = 'https://ws-eu1.brightpearl.com/2.0.0/REMOVED/warehouse-service/goods-note/goods-out/'.$sql_array['OrderNo'];
$headers = array(
"brightpearl-auth: {$authorisationToken}",
'Content-Type: application/json;charset=UTF-8',
'X-HTTP-Method-Override: PUT',
);
$newShippingWarehouseDetails = array(
"priority" => false,
"shipping" =>
array(
"reference" =>'"'.$sql_array['InvoiceNumber'].'"',
),
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $ShippingGoodsoutURL);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($newShippingWarehouseDetails));
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($newShippingWarehouseDetails));
$GoodsoutreShippingResponse = curl_exec($ch);
curl_close($ch);
print_r($GoodsoutreShippingResponse);
//exit;
// End
}
?>
你爲什麼要做'@mysql_error()'?刪除那些'@'。你需要處理你的錯誤,而不是忽略它們。 –
不完全確定,但不應該這一行 - >'curl_setopt($ ch,CURLOPT_POSTFIELDS,http_build_query($ newShippingWarehouseDetails));'使用'json_encode'而不是'http_build_query'。您傳遞的是Content-Type:application/json; charset = UTF-8'類型標題,但將數據作爲編碼的url字符串傳遞。 – War10ck