I'm trying to work out a call to FastCourier API https://enterprise.fastcourier.com.au/docs/api to get a simple quote but it keeps throwing me the error "Invalid ip accessing app" when it's set to the server IP on my https://enterprise.fastcourier.com.au/account/developer-settings.
$apiKey = 'MY_API_KEY';
$url2 = 'https://enterprise-api.fastcourier.com.au/api/quotes';
$ch2 = curl_init();
curl_setopt($ch2,CURLOPT_URL, $url2);
// Create an associative array containing the POST data
$post_data = array(
"quote" => array(
"dropoff_postcode" => "2259",
"dropoff_state" => "NSW",
"dropoff_suburb" => "TUGGERAWONG",
"parcel_attributes" => array(
"qty" => "1",
"weight" => "10.6")
)
);
$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";
Any help welcomed!
This answer is provided by AskSia!
The provided code initializes a cURL session, sets various options including the URL, HTTP headers, and POST fields, executes the cURL request, and then handles the response or errors. It also retrieves and displays the server's IP address.
Step-by-step Instruction
curl_init().curl_setopt()with theCURLOPT_URLoption.json_encode().curl_setopt()with theCURLOPT_HTTPHEADERoption.curl_setopt()with theCURLOPT_CUSTOMREQUESToption.curl_setopt()with theCURLOPT_POSTFIELDSoption.curl_exec()usingcurl_setopt()with theCURLOPT_RETURNTRANSFERoption.curl_exec()and handle the response or errors.curl_close().$_SERVERsuperglobal array.