Skip to table dataSkip to menuSkip to searchHotkeys reference
Sometimes it's difficult to control how much information is sent in response to an API request.
The script below, run from a PHP-equipped server or workstation, can filter the response data.
It reads the response in to an array, filters it, and converts the minimized result to JSON.
It operates recursively and can be modified to filter data from differently structured arrays.

<?php
//This script shows how to filter a large result set, or response, returned from an API.
//Commenting and uncommenting print_r, var_dump, print, echo lines makes it easy to examine responses in detail.
//It's currently configured for the data returned from the uncommented $url variable on line 13.
//Change the $url to a different API. The print command on line 26 makes it easy to examine the response.
//The search function on line 29 filters the response array by variables supplied via $_GET["VARIABLE"].
//The walk_recursive_remove function on line 43 removes key-value pairs with empty values.
//The search_through_array function on line 61 extracts a single record from the array.

$manufacturerid = $_GET["ManufacturerId"];
//$manufacturername = $_GET["ManufacturerName"];
$name = $_GET["Name"];
$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);
$result = json_decode($json_response, TRUE);
//print_r ($result);
print("<pre>".print_r($result, TRUE)."</pre>");
print("<br><br>");
$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',"$manufacturerid");
//$res = search($arr, 'ManufacturerName','MERCEDES-BENZ OF NORTH AMERICA, INC.');
//$res = search($arr, 'ManufacturerName',"$manufacturername");
//$res = search($arr, 'Name','ORG10626');
//$res = search($arr, 'Name',"$name");
//print_r ($res);
//print("<pre>".print_r($res, TRUE)."</pre>");
//print("<br><br>");
function walk_recursive_remove (array $array, callable $callback) {
foreach ($array as $k => $v) {
if (is_array($v)) {
$array[$k] = walk_recursive_remove($v, $callback);
} else {
if ($callback($v, $k)) {
unset($array[$k]); }}}
return $array;
}
$arr = $res;
$result = walk_recursive_remove($arr, function ($value) {
return $value === null || $value === '';
});
//var_dump($result);
//print_r ($result);
print("<pre>".print_r($result, TRUE)."</pre>");
//$data = $res;
$data = $result;
function search_through_array($search,array $lists){
try{
foreach ($lists as $key => $value) {
if(is_array($value)){
array_walk_recursive($value, function($v, $k) use($search ,$key,$value,&$val){
if(strpos($v, $search) !== false ) $val[$key]=$value;});
}else{
if(strpos($value, $search) !== false ) $val[$key]=$value;}}
return $val;}catch (Exception $e) {return false;}
}
print("<br><br>");
//print_r(search_through_array('ORG10614',$data));
$new = search_through_array("$name",$data);
$Package = json_encode($new,TRUE);
echo $Package;
//header("Location: https://postback.goformz.com/webviewreturn?Package=".$Package);
//*/
?>