I’m new to API calls and have limited PHP / JSON knowledge.
I’m trying to retrieve UK mp details via an API call. Using (https://members-api.parliament.uk/index.html) and API call, I’m looking to get the full title of my MP.
I have the code below. Using a callAPI function, the call works. I’m struggling with retrieving the data from the JSON file. I’ve tried looping through the response array and can’t get the attribute I’m looking for, which is the "nameFullTitle."
var_dump
returns all the data, so the call is working; it’s more about finding the data from the JSON file that I’m struggling with.
function callAPI($method, $url, $data)
{
$curl = curl_init();
switch ($method) {
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data) {
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
break;
case "PUT":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
if ($data) {
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
break;
default:
if ($data) {
$url = sprintf("%s?%s", $url, http_build_query($data));
}
echo $url;
echo '<br>';
}
// OPTIONS:
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'APIKEY: 111111111111111111111',
'Content-Type: application/json',
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// EXECUTE:
$result = curl_exec($curl);
if (!$result) {
die("Connection Failure");
}
curl_close($curl);
echo 'complete';
return $result;
}
$url = 'https://members-api.parliament.uk/api/Location/Constituency/Search?searchText=KY11%201NF&skip=0&take=20';
$get_data = callAPI('GET', $url, false);
echo '<br>';
$response = json_decode($get_data, true);
$errors = $response['response']['errors'];
$data = $response['response']['items']['id'][0];
echo $response;
foreach ($response as $x1) {
echo "$x1 x1<br>";
foreach ($x1 as $x2) {
echo "$x2 x2<br>";
foreach ($x2 as $x3){
echo "$x3 x3<br>";
}
}
}
echo '<br><br>';
echo var_dump(json_decode($get_data));
echo '<br>';
echo 'check data is next row<br>';
echo $data. 'this is the data';
echo $errors;
?>
I have tried the below to loop through the array and get the data attribute looking for.
$url = 'https://members-api.parliament.uk/api/Location/Constituency/Search?searchText=KY11%201NF&skip=0&take=20';
$get_data = callAPI('GET', $url, false);
echo '<br>';
$response = json_decode($get_data, true);
$errors = $response['response']['errors'];
$data = $response['response']['items']['id'][0];
echo $response;
foreach ($response as $x1) {
echo "$x1 x1<br>";
foreach ($x1 as $x2) {
echo "$x2 x2<br>";
foreach ($x2 as $x3) {
echo "$x3 x3<br>";
}
}
}
echo '<br><br>';
echo var_dump(json_decode($get_data));
echo '<br>';
echo 'check data is next row<br>';
echo $data.'this is the data';
echo $errors;
2
Answers
Ignoring the curl code used to return the response and concentrating on processing the returned data itself (assuming that the curl code does return data )
It is easier and quicker to use Object notation to access properties within the returned data than array syntax (imo) – hence not supplying
true
as second argument tojson_decode
Choosing a different postcode and hence constituency yielded json as follows:
Which, when processed as above, yielded:
please find below the code