Jump to content


New Members
  • Posts

  • Joined

  • Last visited

GalenAltaiir's Achievements


Newbie (1/5)



  1. Thank you! The API array is massive, so I was having a really hard time understanding it. Putting it like I did here, and having you explain it helped a lot. The code works now, and I got the result I wanted
  2. I tried different ways of accessing the "address", I tried $array["rows"][0] $array["rows"]["address"] but those didn't work. I'm just not sure how to print out the specific elements from columns and rows. The API gives me the json file in that format when I print it as a string, although I see I made a mistake when recreating the string. It looks more like this - $json_string = '{ "column-names":["address1","address2","address3","postcode", ...], "rows": [{"address":"123, Imaginary Road", "someotherdata": dataset, ..., "postcode":"H3L L00" }]}';
  3. Awesome thanks, what should I do this if the json string was formatted bit more like this - $json_string = '{"column-names":["address1","address2","address3","postcode", ...],"rows":["address":"123, Imaginary Road", "someotherdata": dataset, ..., "postcode":"H3L L00"]}'; and I wanted to print the address, and then a post code? The method you provided doesn't seem to work in that scenario (unless I'm formatting it wrong) printf('All done! Welcome to Homepage %s',$array["rows"][0]);
  4. Hi I have a json file that I called from an API into a string. But I noticed that that the name for the array was 2 words seperated with a "-", I understand that normally I'm able to access the array and print it using $object->ArrayName[index] but what do I do if the array if the array name contains a dash? $object->Array-Name[index] I'm fairly new to PHP and a complete begginer with JSON so sorry if this is a bit confusing. I wrote some simple code to recreate the issue I have. Code: $json_string = '{"name":"Jeff","age":20,"active":true,"column-names":["title1","title2"]}'; $object = json_decode($json_string); printf('All done! Welcome to Homepage %s',$object->"column-names"[1]); Output: Parse error: syntax error, unexpected '"column-names"' (T_CONSTANT_ENCAPSED_STRING), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in FILE_LOCATION on line 14
  5. 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
  6. 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
  7. 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?
  8. 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
  • 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.