Main Page
 The gatekeeper of reality is
 quantified imagination.

Stay notified when site changes by adding your email address:

Your Email:

Bookmark and Share
Email Notification
Project JSON
Purpose
The purpose of this project is to demonstrate how to use PHP and CURL to read JSON from an end point. In order to request data from an end-point you may end up using GET, POST or POST formatted as a JSON message. The blocks below will show each of these methods. The last block on this page will show you how you can read the response headers from the request end point that you contact.

Making a GET Request
$err_status = "";
/* Define the URL endpoint where you will get JSON data from */
$apifullurl = "https://www.somesite.com/endpoint";
/* Since we are using GET, attach the name-values to the endpoint URL */
$apifullurl = $apifullurl . "?param1=" . urlencode($param1value) . "&param2=" . urlencode($param2value) . "&param3=" . urlencode($param3value);
/* Create the CURL Handler */
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $apifullurl);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_MAXREDIRS, 2);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); /* Follow redirects, the number of which is defined in CURLOPT_MAXREDIRS */
curl_setopt($curl, CURLOPT_FORBID_REUSE, TRUE);
curl_setopt($curl, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($curl);
$err_status = curl_error($curl);
curl_close($curl);
/* Handle Response */
if (strlen($err_status) == 0) {
if (strlen($response) > 0) {
/* Read the JSON, if any */
$apiresponse = json_decode($response, TRUE);
if (sizeof($apiresponse) > 0) {
/* There may be other data aside from the main JSON message, in the JSON response so the example below just shows getting a simple name-value pair */
$apiresponsecode = $apiresponse["responsecode"];
/* The main JSON message (it may or may not be referenced as "results" in your case) */
for ($j = 0; $j < sizeof($apiresponse["results"]); $j++) {
/* Get name-value data of this array object */
if (isset($apiresponse["results"][$j]["model"])) { print "Model = " . htmlspecialchars($apiresponse["results"][$j]["model"]) . "<br />"; }
if (isset($apiresponse["results"][$j]["description"])) { print "Description = " . htmlspecialchars($apiresponse["results"][$j]["description"]) . "<br />"; }
/* An array may be embedded with this array object; if so, grab that array data */
if (isset($apiresponse["results"][$j]["attributes"])) {
$attributes = Array();
$attributes = $apiresponse["results"][$j]["attributes"];
/* Step through array data */
for ($a = 0; $a < sizeof($attributes); $a++) {
$attributeset = Array();
$attributeset = $attributes[$a];
foreach ($attributeset AS $name => $value) {
print "Name = " . htmlspecialchars($name) . ", Value = " . htmlspecialchars($value) . "<br />";
}
unset($attributeset);
}
unset($attributes);
}
}
}
else {
print "We got a response but it does not appear to be JSON.";
}
}
else {
print "There was no error but there was no response either.";
}
}
else {
print $err_status;
}


Making a POST Request
$err_status = "";
/* Define the URL endpoint where you will get JSON data from */
$apifullurl = "https://www.somesite.com/endpoint";
/* Define an array with which to place name-value data you wish to send */
$apipostdata = Array();
/* Add data to that array */
$apipostdata = Array(
'param1' => $param1value
,'param2' => $param2value
,'param3' => $param3value
);
/* Create the CURL Handler */
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $apifullurl);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_MAXREDIRS, 2);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); /* Follow redirects, the number of which is defined in CURLOPT_MAXREDIRS */
curl_setopt($curl, CURLOPT_FORBID_REUSE, TRUE);
curl_setopt($curl, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($apipostdata));
$response = curl_exec($curl);
$err_status = curl_error($curl);
curl_close($curl);
/* Handle Response */
if (strlen($err_status) == 0) {
if (strlen($response) > 0) {
/* Read the JSON, if any */
$apiresponse = json_decode($response, TRUE);
if (sizeof($apiresponse) > 0) {
/* There may be other data aside from the main JSON message, in the JSON response so the example below just shows getting a simple name-value pair */
$apiresponsecode = $apiresponse["responsecode"];
/* The main JSON message (it may or may not be referenced as "results" in your case) */
for ($j = 0; $j < sizeof($apiresponse["results"]); $j++) {
/* Get name-value data of this array object */
if (isset($apiresponse["results"][$j]["model"])) { print "Model = " . htmlspecialchars($apiresponse["results"][$j]["model"]) . "<br />"; }
if (isset($apiresponse["results"][$j]["description"])) { print "Description = " . htmlspecialchars($apiresponse["results"][$j]["description"]) . "<br />"; }
/* An array may be embedded with this array object; if so, grab that array data */
if (isset($apiresponse["results"][$j]["attributes"])) {
$attributes = Array();
$attributes = $apiresponse["results"][$j]["attributes"];
/* Step through array data */
for ($a = 0; $a < sizeof($attributes); $a++) {
$attributeset = Array();
$attributeset = $attributes[$a];
foreach ($attributeset AS $name => $value) {
print "Name = " . htmlspecialchars($name) . ", Value = " . htmlspecialchars($value) . "<br />";
}
unset($attributeset);
}
unset($attributes);
}
}
}
else {
print "We got a response but it does not appear to be JSON.";
}
}
else {
print "There was no error but there was no response either.";
}
}
else {
print $err_status;
}


Making a POST Request Formatted as JSON
$err_status = "";
/* Define the URL endpoint where you will get JSON data from */
$apifullurl = "https://www.somesite.com/endpoint";
/* Define an array with which to place name-value data you wish to send */
$apipostdata = Array();
$apipostdataencoded = "";
/* Add data to that array */
$apipostdata = Array(
'param1' => $param1value
,'param2' => $param2value
,'param3' => $param3value
);
/* JSON Specific Request Search Parameters Modification */
$tmp_apipostdata = Array();
$tmp_apipostdata = $apipostdata;
unset($apipostdata);
$apipostdataencoded = json_encode((array)$tmp_apipostdata);
unset($tmp_apipostdata);
/* JSON Request Specific Content Header */
$apihttpheader = Array(
'Content-Type: application/json'
,'Accept: application/json'
,'Content-Length: ' . strlen($apipostdataencoded)
);
/* Create the CURL Handler */
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $apifullurl);
curl_setopt($curl, CURLOPT_HTTPHEADER, $apihttpheader);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_MAXREDIRS, 2);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); /* Follow redirects, the number of which is defined in CURLOPT_MAXREDIRS */
curl_setopt($curl, CURLOPT_FORBID_REUSE, TRUE);
curl_setopt($curl, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $apipostdataencoded);
$response = curl_exec($curl);
$err_status = curl_error($curl);
curl_close($curl);
/* Handle Response */
if (strlen($err_status) == 0) {
if (strlen($response) > 0) {
/* Read the JSON, if any */
$apiresponse = json_decode($response, TRUE);
if (sizeof($apiresponse) > 0) {
/* There may be other data aside from the main JSON message, in the JSON response so the example below just shows getting a simple name-value pair */
$apiresponsecode = $apiresponse["responsecode"];
/* The main JSON message (it may or may not be referenced as "results" in your case) */
for ($j = 0; $j < sizeof($apiresponse["results"]); $j++) {
/* Get name-value data of this array object */
if (isset($apiresponse["results"][$j]["model"])) { print "Model = " . htmlspecialchars($apiresponse["results"][$j]["model"]) . "<br />"; }
if (isset($apiresponse["results"][$j]["description"])) { print "Description = " . htmlspecialchars($apiresponse["results"][$j]["description"]) . "<br />"; }
/* An array may be embedded with this array object; if so, grab that array data */
if (isset($apiresponse["results"][$j]["attributes"])) {
$attributes = Array();
$attributes = $apiresponse["results"][$j]["attributes"];
/* Step through array data */
for ($a = 0; $a < sizeof($attributes); $a++) {
$attributeset = Array();
$attributeset = $attributes[$a];
foreach ($attributeset AS $name => $value) {
print "Name = " . htmlspecialchars($name) . ", Value = " . htmlspecialchars($value) . "<br />";
}
unset($attributeset);
}
unset($attributes);
}
}
}
else {
print "We got a response but it does not appear to be JSON.";
}
}
else {
print "There was no error but there was no response either.";
}
}
else {
print $err_status;
}


Grabbing Response Header Data
$err_status = "";
$header_size = 0;
/* Define the URL endpoint where you will get header data from */
$apifullurl = "https://www.somesite.com/endpoint";
/* Create the CURL Handler */
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $apifullurl);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_MAXREDIRS, 2);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); /* Follow redirects, the number of which is defined in CURLOPT_MAXREDIRS */
curl_setopt($curl, CURLOPT_FORBID_REUSE, TRUE);
curl_setopt($curl, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_VERBOSE, TRUE);
curl_setopt($curl, CURLOPT_HEADER, TRUE);
$response = curl_exec($curl);
$err_status = curl_error($curl);
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
curl_close($curl);
/* Handle Response */
$header = substr($response, 0, $header_size);
print $header;


About Joe