我们如何从API中检索值

时间:2014-09-03 03:37:19

标签: php api curl

我遇到了麻烦。从HunterExpress API,我想获取传递postCode的运费,产品尺寸/重量等我使用以下代码来获取运费

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

但它没有显示任何结果且没有错误

2 个答案:

答案 0 :(得分:1)

因为您没有初始化您的cURL资源。你的curl_init()在哪里?

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

同时修复我在上面评论中提到的语法错误。还要检查错误

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

答案 1 :(得分:0)

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

检查是否启用了curl_function