Skip to table dataSkip to menuSkip to searchHotkeys reference
The first 5 scripts all request data, read the response, filter, extract, and format the result(s).
The last 2 scripts either send a personalized MMS message or call a recipient with a pre-recorded message.
Save individually as UTF-8 encoded text files with .php extension (apiquery.php for example) to a PHP-equipped environment to test.
The first script shares my app_id, please use sparingly.

<?php
//This script extracts a currency exchange rate from an API response based on country code supplied in via the URL.
//Commenting and uncommenting print_r, var_dump, print, echo lines makes it easy to examine the response in detail.
//Used as is it outputs 4 versions of the JSON returned so you can see how the array of data received is organized.
//It also outputs 4 versions of the result of looking up an exchange rate by country code and formatting the result.
//Use "http://YOURSERVER/apiquery.php?code=CAD" to fetch Canadian rate. Change CAD to fetch rates for other countries.
$code = $_GET["code"];
$url = "https://openexchangerates.org/api/latest.json?app_id=be06ded6776d499da2d98cac0f2612f4&base=USD";
$curl = curl_init( $url );
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$json_response = curl_exec($curl);
curl_close($curl);
print("JSON response"."<br>");
print_r ($json_response);
print("<br><br>"."JSON response decoded to a PHP array"."<br>");
$result = json_decode($json_response, TRUE);
print_r ($result);
print("<br><br>"."Variables dump of PHP array"."<br>");
var_dump($result);
print_r($result)."<br><br>";
print("<br><br>"."PHP array formatted for legibilty"."<br>");
print("<pre>".print_r($result, TRUE)."</pre>");
print("The result of searching array for Currency Code (http://localhost/apiquery.php?code=$code) "."<br>");
$arr = $result;
echo $arr['rates']["$code"];
print("<br><br>");
$json = json_encode($arr['rates']["$code"]);
print("The result of JSON encoding the unformatted result"."<br>");
echo $json;
print("<br><br>");
$jsoncustomformat = "[{\"rate\":\"$json\"}]";
print("The result of adding custom formatting to the result"."<br>");
echo $jsoncustomformat;
print("<br><br>");
$json = json_encode($jsoncustomformat);
print("The result of JSON encoding the formatted result"."<br>");
echo $json;
print("<br><br><br>");
?>

<?php
//This script extracts different components from responses from 6 different APIs.
//Comment and uncomment print_r, var_dump, print lines to examine responses in various formats.
//Comment and uncomment $url and corresponding foreach line to test.
//Change $res = search($arr, 'KEY',VALUE) when target values are numbers.
//Change $res = search($arr, 'KEY','VALUE') when target values are strings.
//Change $res = search($arr, 'KEY',"$VALUE") when target values are variables.
//Change [$key] === $value) to [$key] == $value) to relax type check
///*

$url = "https://vpic.nhtsa.dot.gov/api/vehicles/GetParts?type=565&fromDate=1/1/2015&toDate=5/5/2015&format=json";
//$url = "https://restcountries.eu/rest/v2/all?fields=name;capital;region;subregion;population;area;currencies";
//$url = "https://api.coinlore.net/api/tickers/";
//$url = "https://api.publicapis.org/entries?https=true";
//$url = "http://ergast.com/api/f1/circuits.json";
//$url = "https://api.nytimes.com/svc/news/v3/content/all/all.json?api-key=gb5yAzGoOvnG7G3TmrGninqF9flFlQrt";

$curl = curl_init( $url );
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$json_response = curl_exec($curl);
curl_close($curl);
//print_r ($json_response);
$result = json_decode($json_response, TRUE);
//var_dump($result);
//print_r ($result);
//print("<pre>".print_r($result, TRUE)."</pre>");
$arr = $result;
function search($array, $key, $value) {$results = array();
if (is_array($array)) {
if (isset($array[$key]) && $array[$key] === $value) {$results[] = $array;}
foreach ($array as $subarray) { $results = array_merge($results,search($subarray, $key, $value));}}
return $results;}
$res = search($arr, 'ManufacturerId',1152);
//$res = search($arr, 'ManufacturerName','MERCEDES-BENZ OF NORTH AMERICA, INC.');
foreach ($res as $var) {echo $var["ManufacturerId"]." - ".$var["ManufacturerName"]." - ".$var["LetterDate"]." - ".$var['Name']." - ".$var['URL'] ."<br>"; };
//foreach ($res as $var) {echo $var["name"]." - ".$var["capital"]." - ".$var["region"]." - ".$var['subregion']." - ".$var['population'] . " - ". $var['area'] ."<br>"; };
//foreach ($res as $var) {echo $var["id"]." - ".$var["name"]." - ".$var["symbol"]." - ".$var["rank"]." - ".$var['price_usd']." - ".$var['price_btc'] ."<br>"; };
//foreach ($res as $var) {echo $var["API"]." - ".$var["Description"]." - ".$var["Auth"]." - ".$var["HTTPS"]." - ".$var['Cors']." - ".$var['Link'] ."<br>"; };
//foreach ($res as $var) {echo $var["circuitId"]." - ".$var["url"]." - ".$var["circuitName"]."<br>"; };
//foreach ($res as $var) {echo $var["slug_name"]." - ".$var["section"]." - ".$var["title"]."<br>"; };
print("<br><br>");
//*/
?>

<?php
//This script is modified from the script above to convert an API query result to JSON.
//It's output is packaged as JSON for interaction with other services, a GoFormz form in this example.
/*
$url = "https://vpic.nhtsa.dot.gov/api/vehicles/GetParts?type=565&fromDate=1/1/2015&toDate=5/5/2015&format=json";
$curl = curl_init( $url );
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$json_response = curl_exec($curl);
curl_close($curl);
$result = json_decode($json_response, TRUE);
$arr = $result;
function search($array, $key, $value) {$results = array();
if (is_array($array)) {
if (isset($array[$key]) && $array[$key] === $value) {$results[] = $array;}
foreach ($array as $subarray) { $results = array_merge($results,search($subarray, $key, $value));}}
return $results;}
//$res = search($arr, 'ManufacturerId',1152);
$res = search($arr, 'ManufacturerName','MERCEDES-BENZ OF NORTH AMERICA, INC.');
$Package = json_encode($res);
header("Location: https://postback.goformz.com/webviewreturn?Package=".$Package);
*/
?>

<?php
//This script is modified from the script above to use a searchterm supplied in a URL parameter
//Use "http://YOURSERVER/apiquery.php?searchterm=1152" to fetch records by ManufacturerID in this example.
/*
$searchterm = $_GET["searchterm"];
$url = "https://vpic.nhtsa.dot.gov/api/vehicles/GetParts?type=565&fromDate=1/1/2015&toDate=5/5/2015&format=json";
$curl = curl_init( $url );
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$json_response = curl_exec($curl);
curl_close($curl);
$result = json_decode($json_response, TRUE);
$arr = $result;
function search($array, $key, $value) {$results = array();
if (is_array($array)) {
if (isset($array[$key]) && $array[$key] == $value) {$results[] = $array;}
foreach ($array as $subarray) { $results = array_merge($results,search($subarray, $key, $value));}}
return $results;}
//$res = search($arr, 'ManufacturerId',1152);
$res = search($arr, 'ManufacturerId',"$searchterm");
//$res = search($arr, 'ManufacturerName','MERCEDES-BENZ OF NORTH AMERICA, INC.');
//$res = search($arr, 'ManufacturerName',"$searchterm");
//print_r ($res);
$Package = json_encode($res);
echo $Package;
//header("Location: https://postback.goformz.com/webviewreturn?Package=".$Package);
*/
?>

<?php
//This script loads a JSON file, builds a multidimensional associative array of the data, and filters it to extract desired information.
//Adapted from "PHP 7 Solutions" by David Powers - ISBN 978-1-4842-4338-1 where functionality is fully explained. Excellent book, highly recommended.
//This is configured to run locally. Install XAMPP, start Apache, save to "C:\xampp\htdocs\apiquery.php", test with "http://localhost/apiquery.php".
//Save JSON returned from https://data.sfgov.org/api/views/yitu-d5am/rows.json?accessType=DOWNLOAD to "C:\xampp\htdocs\data\film_locations.json"
//Use UTF-8 Encoding!!!).
//Change $search and $location[''] variables to search parameter and target column name.

$json = file_get_contents('./data/film_locations.json');
$data = json_decode($json, true);
$result = $data;
//var_dump($result);
//print_r ($result);
//print("<pre>".print_r($result, TRUE)."</pre>");
$col_names = array_column($data['meta']['view']['columns'], 'name');
$locations = [];
foreach ($data['data'] as $datum) {
$locations[] = array_combine($col_names, $datum);}
//print("<pre>".print_r($locations, TRUE)."</pre>");
$search = 'Columbia';
$getLocation = function ($location) use ($search) {
return (stripos($location['Production Company'], $search) !== false);};
//$search = '2004';
//$getLocation = function ($location) use ($search) {
// return (stripos($location['Release Year'], $search) !== false);};
$filtered = array_filter($locations, $getLocation);
echo '<ul>';
foreach ($filtered as $item) {
echo "<li>{$item['Title']} ({$item['Release Year']}),({$item['Production Company']}) filmed at {$item['Locations']}</li>";}
echo '</ul>';
?>


<?php
//This script uses the Twilio API send a personalized MMS message to a designated recipient.
//This is configured to run locally. Install XAMPP, start Apache, save to "C:\xampp\htdocs\mms.php", test with "http://localhost/mms.php".
//It requires the Twilio PHP Helper library be installed, see "https://www.twilio.com/docs/libraries/php"
//For more info on the Twilio SMS API for PHP see "https://www.twilio.com/docs/sms/quickstart/php"
//Replace the $sid and $token variables with an active Twilio account's credentials
require __DIR__ . '/twilio-php-main/src/Twilio/autoload.php';
use Twilio\Rest\Client;
$recipient = $_GET["Recipient"];
$name = $_GET["Name"];
$sid = 'EXAMPLE5b4f3eb72fbea97532fe7c0439c';
$token = 'EXAMPLE59a2353a265babc031c730d15';
$client = new Client($sid, $token);
$client->messages->create(
"$recipient",
[
'from' => '+13262224991',
'body' => "Hey $name! Check out this owl!",
"mediaUrl" => ["https://demo.twilio.com/owl.png"]
]
);
header("Location: https://postback.goformz.com/webviewreturn?Result=MessageSent");
?>

<?php
//This script uses the Twilio API to send a personalized MMS message to a designated recipient.
//This is configured to run locally. Install XAMPP, start Apache, save to "C:\xampp\htdocs\voice.php", test with "http://localhost/voice.php".
//It requires the Twilio PHP Helper library be installed, see "https://www.twilio.com/docs/libraries/php"
//For more info on Twilio Programmable Voice API for PHP see "https://www.twilio.com/docs/voice/quickstart/php"
//Replace the $sid and $token variables with an active Twilio account's credentials
require __DIR__ . '/twilio-php-main/src/Twilio/autoload.php';
use Twilio\Rest\Client;
$recipient = $_GET["Recipient"];
$sid = "EXAMPLE5b4f3eb72fbea97532fe7c0439c";
$token = "EXAMPLE59a2353a265babc031c730d15";
$client = new Twilio\Rest\Client($sid, $token);
$call = $client->calls->create(
"$recipient",
'13262224991',
[
'url' => 'https://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient'
]
);
header("Location: https://postback.goformz.com/webviewreturn?Result=CallCompleted");