Jump to content

Calling an API and Testing within PHP (noobie help needed)


Go to solution Solved by maxxd,

Recommended Posts

Hi, I'm a learning PHP developer, and I'm trying to work out how to connect and use an API provided by my goverment to the public. 

I have no idea how to set it up and test it, and I'm having a really hard time finding any help online.

The documentation mentions using a "simple" cURL request but I have no idea how that works either, and I would like ideally to be able to pull the data and turn into an SQL database where I know how things work a little bit better within PHP.

I would be really grateful if someone could explain how to connect and use this API with some simple code to get me started. There is a token, and a key involved so I would like to know how to verify myself for the API as well.

You can find the access to the API here: https://epc.opendatacommunities.org/login
and the documentation here: https://epc.opendatacommunities.org/docs/api/domestic

 

Many thankss

  • Solution

You can use plain php curl, but honestly I think it'll be easier to deal with if you use Guzzle. I find it much easier, especially when dealing with passing tokens and keys and whatnot; as the docs say:

$client = new GuzzleHttp\Client();
$res = $client->request('GET', 'https://api.github.com/user', [
    'auth' => ['user', 'pass']
]);
echo $res->getStatusCode();
// "200"
echo $res->getHeader('content-type')[0];
// 'application/json; charset=utf8'
echo $res->getBody();
// {"type":"User"...'

Give it a shot and come back with code if something goes weird.

2 hours ago, maxxd said:

You can use plain php curl, but honestly I think it'll be easier to deal with if you use Guzzle. I find it much easier, especially when dealing with passing tokens and keys and whatnot; as the docs say:

$client = new GuzzleHttp\Client();
$res = $client->request('GET', 'https://api.github.com/user', [
    'auth' => ['user', 'pass']
]);
echo $res->getStatusCode();
// "200"
echo $res->getHeader('content-type')[0];
// 'application/json; charset=utf8'
echo $res->getBody();
// {"type":"User"...'

Give it a shot and come back with code if something goes weird.

Thanks! I'll definately look into Guzzle. I actually managed to the API working using php curl, which took some reading and a curl to php curl converter (hence why i'll be sticking with that for now).

I worked out how to search for specific address (the database contains home addresses as well as various ratings on the property like windows, walls, gas, any smart features etc.)
 

But now I'm trying to work out how to do 2 things,

- How do I modify the code so it only shows me relevant set of information

- How do I take the output, and divide it so I can either put it as a table to display on a page, or to store certain strings as variables.

 

This is the code I have at the moment:

 

$url = "https://epc.opendatacommunities.org/api/v1/domestic/search?address=".$address."";


$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$headers = array(
   "Accept: text/csv",
   "Authorization: {Token}",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);


$resp = curl_exec($curl);
curl_close($curl);

$address is just set to one address I found on the database.

This is the output I get:

Result: string(2572) "lmk-key,address1,address2,address3,postcode,building-reference-number,current-energy-rating,potential-energy-rating,current-energy-efficiency,potential-energy-efficiency,property-type,built-form,inspection-date,local-authority,constituency,county,lodgement-date,transaction-type,environment-impact-current,environment-impact-potential,energy-consumption-current,energy-consumption-potential,co2-emissions-current,co2-emiss-curr-per-floor-area,co2-emissions-potential,lighting-cost-current,lighting-cost-potential,heating-cost-current,heating-cost-potential,hot-water-cost-current,hot-water-cost-potential,total-floor-area,energy-tariff,mains-gas-flag,floor-level,flat-top-storey,flat-storey-count,main-heating-controls,multi-glaze-proportion,glazed-type,glazed-area,extension-count,number-habitable-rooms,number-heated-rooms,low-energy-lighting,number-open-fireplaces,hotwater-description,hot-water-energy-eff,hot-water-env-eff,floor-description,floor-energy-eff,floor-env-eff,windows-description,windows-energy-eff,windows-env-eff,walls-description,walls-energy-eff,walls-env-eff,secondheat-description,sheating-energy-eff,sheating-env-eff,roof-description,roof-energy-eff,roof-env-eff,mainheat-description,mainheat-energy-eff,mainheat-env-eff,mainheatcont-description,mainheatc-energy-eff,mainheatc-env-eff,lighting-description,lighting-energy-eff,lighting-env-eff,main-fuel,wind-turbine-count,heat-loss-corridor,unheated-corridor-length,floor-height,photo-supply,solar-water-heating-flag,mechanical-ventilation,address,local-authority-label,constituency-label,posttown,construction-age-band,lodgement-datetime,tenure,fixed-lighting-outlets-count,low-energy-fixed-light-count 1229391483952014103108154495749528,"142, Edgar Road",,,TW4 5QP,5844359278,D,C,61,73,Maisonette,End-Terrace,2014-10-30,E09000027,E14001005,Greater London Authority,2014-10-31,marketed sale,57,73,241,152,4,46,2.5,56,56,747,473,155,125,87,Single,Y,3rd,Y,,2104,100,double glazing installed during or after 2002,Normal,0,5,5,100,0,From main system,Good,Good,"Solid, no insulation (assumed)",N/A,N/A,Fully double glazed,Good,Good,"Cavity wall, as built, no insulation (assumed)",Poor,Poor,None,N/A,N/A,"Flat, limited insulation (assumed)",Very Poor,Very Poor,"Boiler and radiators, mains gas",Good,Good,Programmer and room thermostat,Average,Average,Low energy lighting in all fixed outlets,Very Good,Very Good,mains gas (not community),1,unheated corridor,5,,0,,natural,"142, Edgar Road",Richmond upon Thames,Twickenham,HOUNSLOW,England and Wales: 1967-1975,2014-10-31 08:15:44,owner-occupied,11,11 "

 

I noticed that I can't store the string that comes out of

var_dump($resp);

as is, I was thinking if I can store the output as a string maybe I can slice it up and then I would have more control over what and how I can display the data to the user?

Edited by GalenAltaiir

Okay, another progress update. I worked out how to put the data into an array. 

	ob_start();
   	var_dump($resp);
    $result = ob_get_clean();


    $resultArray = array(explode(",",$result));

    print_r($resultArray[1]);

but I'm having problems displaying specific parts of the array. When I run the bottom line of that code I get

Notice: Undefined offset: 1 in <file_location> on <line number>

If I remove "[1]" it displays the whole array, but I'm not sure why I can't display the specific parts

I'm guessing that by placing the result of the explode inside the array type you are creating a single element of an unexpected array.  Try this:

$resultarray = explode(',',$result);

That will produce the array you are expecting.  Explode produces an array naturally, no need to do a type on it.

I get the impression that there should be 2 arrays in that data. The first 90 or so elements look like headings and the final 90 look like data. Could be there is something like a "\n" in the middle that isn't showing.

You may need 2 explodes. The first to split the results into records and the second to split those records into fields.

17 hours ago, ginerjm said:

I'm guessing that by placing the result of the explode inside the array type you are creating a single element of an unexpected array.  Try this:

$resultarray = explode(',',$result);

That will produce the array you are expecting.  Explode produces an array naturally, no need to do a type on it.

 

15 hours ago, Barand said:

I get the impression that there should be 2 arrays in that data. The first 90 or so elements look like headings and the final 90 look like data. Could be there is something like a "\n" in the middle that isn't showing.

You may need 2 explodes. The first to split the results into records and the second to split those records into fields.

Thanks a lot for the help guys. I've got everything sorted now thanks to your tips. It went a little bit off-topic from the orignal post as I ran into a few issues, but that's all solved thanks to you lot.

 

For the curious, yeah in the API the first 89 elements were headings, and the remainder was the data for those headings. I had 4 hours total to try this out (it was an open practice task, so asking for help on stuff like PHP or Curl is fine), so I sadly I didn't get to try out using a 2nd explode to split the array better. Instead I just ran a for loop until the midpoint, and then printed all the headings, then offsetting the other half by 89 and printing the 2nd time into a table. This worked pretty well, but a 2nd explode would have saved me some time. 

 

Again thanks for the help :) 

A couple of points about your processing:

1 ) As your first heading is "lmk_key" and the second is "address1", it appears that the data actually starts halfway through element 89, and that somewhere along the way whatever split the headings from the data has become a space character

2 ) Exploding on "," isn't working as the address data itself contains a comma, thus splitting that data over two elements ...

    [89] => low-energy-fixed-light-count 1229391483952014103108154495749528
    [90] => "142
    [91] =>  Edgar Road"

Better to use str_getcsv() which knows how to process ... , "142, Edgar Road", ...

If I change that space in the middle of element 89 to a |, then

$str = 'lmk-key,address1,address2,address3,postcode,building-reference-number,current-energy-rating,potential-energy-rating,current-energy-efficiency,potential-energy-efficiency,property-type,built-form,inspection-date,local-authority,constituency,county,lodgement-date,transaction-type,environment-impact-current,environment-impact-potential,energy-consumption-current,energy-consumption-potential,co2-emissions-current,co2-emiss-curr-per-floor-area,co2-emissions-potential,lighting-cost-current,lighting-cost-potential,heating-cost-current,heating-cost-potential,hot-water-cost-current,hot-water-cost-potential,total-floor-area,energy-tariff,mains-gas-flag,floor-level,flat-top-storey,flat-storey-count,main-heating-controls,multi-glaze-proportion,glazed-type,glazed-area,extension-count,number-habitable-rooms,number-heated-rooms,low-energy-lighting,number-open-fireplaces,hotwater-description,hot-water-energy-eff,hot-water-env-eff,floor-description,floor-energy-eff,floor-env-eff,windows-description,windows-energy-eff,windows-env-eff,walls-description,walls-energy-eff,walls-env-eff,secondheat-description,sheating-energy-eff,sheating-env-eff,roof-description,roof-energy-eff,roof-env-eff,mainheat-description,mainheat-energy-eff,mainheat-env-eff,mainheatcont-description,mainheatc-energy-eff,mainheatc-env-eff,lighting-description,lighting-energy-eff,lighting-env-eff,main-fuel,wind-turbine-count,heat-loss-corridor,unheated-corridor-length,floor-height,photo-supply,solar-water-heating-flag,mechanical-ventilation,address,local-authority-label,constituency-label,posttown,construction-age-band,lodgement-datetime,tenure,fixed-lighting-outlets-count,low-energy-fixed-light-count|1229391483952014103108154495749528,"142, Edgar Road",,,TW4 5QP,5844359278,D,C,61,73,Maisonette,End-Terrace,2014-10-30,E09000027,E14001005,Greater London Authority,2014-10-31,marketed sale,57,73,241,152,4,46,2.5,56,56,747,473,155,125,87,Single,Y,3rd,Y,,2104,100,double glazing installed during or after 2002,Normal,0,5,5,100,0,From main system,Good,Good,"Solid, no insulation (assumed)",N/A,N/A,Fully double glazed,Good,Good,"Cavity wall, as built, no insulation (assumed)",Poor,Poor,None,N/A,N/A,"Flat, limited insulation (assumed)",Very Poor,Very Poor,"Boiler and radiators, mains gas",Good,Good,Programmer and room thermostat,Average,Average,Low energy lighting in all fixed outlets,Very Good,Very Good,mains gas (not community),1,unheated corridor,5,,0,,natural,"142, Edgar Road",Richmond upon Thames,Twickenham,HOUNSLOW,England and Wales: 1967-1975,2014-10-31 08:15:44,owner-occupied,11,11';
$arr = [];
foreach (explode('|', $str) as $line) {
    $arr[] = str_getcsv($line);
}
echo '<pre>' . print_r(array_combine($arr[0], $arr[1]), 1) . '</pre>';

outputting...

Array
(
    [lmk-key] => 1229391483952014103108154495749528
    [address1] => 142, Edgar Road
    [address2] => 
    [address3] => 
    [postcode] => TW4 5QP
    [building-reference-number] => 5844359278
    [current-energy-rating] => D
    [potential-energy-rating] => C
    [current-energy-efficiency] => 61
    [potential-energy-efficiency] => 73
    [property-type] => Maisonette
    [built-form] => End-Terrace
    [inspection-date] => 2014-10-30
    [local-authority] => E09000027
    [constituency] => E14001005
    [county] => Greater London Authority
    [lodgement-date] => 2014-10-31
    .
    .
    .
    etc
}

 

  • Thanks 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.