我正在尝试调用 FastCourier API https://enterprise.fastcourier.com.au/docs/api 以获得简单的报价,但它总是向我抛出错误“无效的 ip 访问应用程序”设置为我的 https://enterprise.fastcourier.com.au/account/developer-settings.
上的服务器 IP$apiKey = 'MY_API_KEY';
$url2 = 'https://enterprise-api.fastcourier.com.au/api/quotes';
$ch2 = curl_init();
curl_setopt($ch2,CURLOPT_URL, $url2);
$post_data = array(
"pickupSuburb" => "TUGGERAWONG",
"pickupState" => "NSW",
"pickupPostcode" => "2259",
"pickupBuildingType" => "residential",
"isPickupTailLift" => false,
"destinationSuburb" => "MELBOURNE",
"destinationState" => "VIC",
"destinationPostcode" => "3000",
"destinationBuildingType" => "residential",
"isDropOffTailLift" => false,
"isDropOffPOBox" => false,
"items" => array(
"type" => "box",
"weight" => "10.6",
"length" => "25",
"width" => "23",
"height" => "20",
"quantity" => "1"
)
);
$data = http_build_query($post_data) . "\n";
curl_setopt($ch2, CURLOPT_HTTPHEADER, array(
'Content-Type:application/json',
'Secret-Key: ' . $apiKey .' '
));
curl_setopt($ch2,CURLOPT_CUSTOMREQUEST,'POST');
curl_setopt($ch2,CURLOPT_POSTFIELDS, $data);
curl_setopt($ch2,CURLOPT_RETURNTRANSFER, true);
$json_response = curl_exec($ch2);
$curl_errno = curl_errno($ch2);
if ($curl_errno > 0) {
echo "cURL Error ($curl_errno): $curl_error\n";
} else {
print_r(json_decode($json_response, true));
}
curl_close($ch2);
// IP address of the server
$ip_server = $_SERVER['SERVER_ADDR'];
echo "<br>Server IP Address is: $ip_server";
欢迎任何帮助!
此答案由AskSia提供!
// Initialize a cURL session
$ch2 = curl_init();
// Set the URL option for a cURL transfer
curl_setopt($ch2,CURLOPT_URL, $url2);
// Create an associative array containing the POST data
$post_data = array(
"pickupSuburb" => "TUGGERAWONG",
"pickupState" => "NSW",
"pickupPostcode" => "2259",
"pickupBuildingType" => "residential",
"isPickupTailLift" => false,
"destinationSuburb" => "MELBOURNE",
"destinationState" => "VIC",
"destinationPostcode" => "3000",
"destinationBuildingType" => "residential",
"isDropOffTailLift" => false,
"isDropOffPOBox" => false,
"items" => array(
"type" => "box",
"weight" => "10.6",
"length" => "25",
"width" => "23",
"height" => "20",
"quantity" => "1"
)
);
// Encode the array into a JSON formatted string
$data = json_encode($post_data);
// Set the HTTPHEADER option for a cURL session
curl_setopt($ch2, CURLOPT_HTTPHEADER, array(
'Content-Type:application/json',
'Secret-Key: ' . $apiKey
));
// Set the custom request type to POST
curl_setopt($ch2,CURLOPT_CUSTOMREQUEST,'POST');
// Set the POST fields for the cURL session
curl_setopt($ch2,CURLOPT_POSTFIELDS, $data);
// Set the option to return the transfer as a string
curl_setopt($ch2,CURLOPT_RETURNTRANSFER, true);
// Execute the cURL session and store the response
$json_response = curl_exec($ch2);
// Check for cURL errors
$curl_errno = curl_errno($ch2);
// Handle cURL errors if any
if ($curl_errno > 0) {
// Output the error message
echo "cURL Error ($curl_errno): $curl_error\n";
} else {
// Decode the JSON response and print it
print_r(json_decode($json_response, true));
}
// Close the cURL session
curl_close($ch2);
// Get the server IP address
$ip_server = $_SERVER['SERVER_ADDR'];
// Output the server IP address
echo "<br>Server IP Address is: $ip_server";
说明 提供的代码初始化 cURL 会话,设置各种选项(包括 URL、HTTP 标头和 POST 字段),执行 cURL 请求,然后处理响应或错误。它还检索并显示服务器的 IP 地址。
分步说明 ▪ ⋮ 使用
curl_init()
初始化cURL 会话。
▪ ⋮ 使用curl_setopt()
和CURLOPT_URL
选项设置cURL 会话的URL。
▪ ⋮ 使用所需的POST 数据创建关联数组。
▪ ⋮ 使用json_encode()
将POST 数据数组编码为JSON 格式的字符串。
▪ ⋮ 使用curl_setopt()
和CURLOPT_HTTPHEADER
选项设置cURL 会话的HTTP 标头。
▪ ⋮ 使用curl_setopt()
和CURLOPT_CUSTOMREQUEST
选项将请求类型设置为POST。
▪ ⋮ 使用curl_setopt()
和CURLOPT_POSTFIELDS
选项设置cURL 会话的POST 字段。
▪ ⋮ 使用 curl_exec()
和 curl_setopt()
选项设置将传输作为 CURLOPT_RETURNTRANSFER
返回值的字符串返回的选项。
▪ ⋮ 使用curl_exec()
执行cURL 会话并处理响应或错误。
▪ ⋮ 使用curl_close()
关闭cURL 会话。
▪ ⋮ 使用 $_SERVER
超全局数组检索并显示服务器的 IP 地址。