2014-09-03 122 views
0

我有麻煩了。從HunterExpress API retieve價值觀,我想爲通過郵政編碼,產品尺寸/重量等我使用下面的代碼來獲取運費我們怎樣才能從API

取運費金額
$data = array(
    'username' => "xxx", 
    'password' => "xxx", 
    'customerCode' => "DUMMY", 
    'fromLocation' => array("suburbName"=> "MELBOURNE", "postCode"=> "3000", "state"=> "VIC"), 
    'toLocation' =>array("suburbName"=> "SYDNEY", "postCode"=> "2000", "state"=> "NSW"), 
    'goods' =>array("pieces"=> "2", 
    'weight' => "3", 
    'width' => "10", 
    'height' => "20", 
    'depth' => "12", 
    'typeCode' => "ENV") 
); 

$url = "https://api_link";//Api Link 
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

$result = curl_exec($ch); 
echo $result; 

但它並沒有顯示出任何結果,並沒有錯誤

+0

結果變量是空的還是什麼? – 2014-09-03 03:39:30

+0

結果變量爲空 – user3931851 2014-09-03 03:41:10

+0

它可能是API,它可能只是因爲身份驗證方法錯誤而丟失請求,請查看返回頭文件。 – cujo 2014-09-03 03:41:14

回答

0
if(!function_exists('curl_version')) 
    die("Curl function is disabled"); 

或者檢查是否啓用curl_function不

+0

我用上面的代碼檢查了curl函數是否啓用 – user3931851 2014-09-03 03:43:03

+0

是$ ch = curl_init() ;定義了? – Parfait 2014-09-03 03:46:54

1

因爲你沒有初始化捲曲資源。你的curl_init()在哪裏?

$ch = curl_init();  // without this, there is no cURL to execute 

還修復了我在上面評論中提到的語法錯誤。還要檢查錯誤

if(curl_exec($ch) === false) 
{ 
    echo 'Curl error: ' . curl_error($ch); 
} 
+0

當我使用上面的代碼時它顯示「Curl錯誤:SSL證書問題,請驗證CA證書是否正常。詳細信息:錯誤:14090086:SSL例程:SSL3_GET_SERVER_CERTIFICATE:證書驗證失敗「 – user3931851 2014-09-03 03:50:31

+1

然後,您還需要爲您的cURL調用禁用SSL驗證選項,您可以爲此發出另一個'setopt'調用。 /問題/ 6400300/HTTPS-和SSL3-GET-服務器certificatecertificate-驗證失敗的-CA-是-OK – 2014-09-03 03:52:20