FastCourier API - Invalid ip accessing app

23 Views Asked by At

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!

1

There are 1 best solutions below

0
AskSia On

This answer is provided by 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"; 

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

  • Initialize a cURL session using curl_init().
  • Set the URL for the cURL session using curl_setopt() with the CURLOPT_URL option.
  • Create an associative array with the required POST data.
  • Encode the POST data array into a JSON formatted string using json_encode().
  • Set the HTTP headers for the cURL session using curl_setopt() with the CURLOPT_HTTPHEADER option.
  • Set the request type to POST using curl_setopt() with the CURLOPT_CUSTOMREQUEST option.
  • Set the POST fields for the cURL session using curl_setopt() with the CURLOPT_POSTFIELDS option.
  • Set the option to return the transfer as a string of the return value of curl_exec() using curl_setopt() with the CURLOPT_RETURNTRANSFER option.
  • Execute the cURL session using curl_exec() and handle the response or errors.
  • Close the cURL session using curl_close().
  • Retrieve and display the server's IP address using the $_SERVER superglobal array.