ovic ovic - 11 months ago 181
JSON Question

Trying to get API output in PHP

I am trying to get output of this API using php. It's a Australia Post Freight calculator. I am not sure what is wrong with it, can some one please suggest. It will be really helpful.

// Set your API key: remember to change this to your live API key in production
$apiKey = 'a4951431-3b25-4c4a-a2d3-4eb03342c78a';

// Set the URL for the Domestic Parcel Size service
$urlPrefix = 'digitalapi.auspost.com.au';
$parcelTypesURL = 'https://' . $urlPrefix . '/postage/parcel/domestic/size.json';

// Lookup domestic parcel types (different kinds of standard boxes etc)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $parcelTypesURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('AUTH-KEY: ' . $apiKey));
$rawBody = curl_exec($ch);

// Check the response: if the body is empty then an error occurred
if(!$rawBody){
die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}

// All good, lets parse the response into a JSON object
$parcelTypesJSON = json_decode($rawBody);

Answer Source

curl_init() is disabled for security reasons...

This means that the server has disabled that function.
If you have control of the server, then enable curl_init() in the php.ini. More information here.
If you do not, try using file_get_contents(). more information here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download