Hi, I am using curl which i have never used before to return a value from a url. What i have at the moment is: $ch = curl_init("http://www.myurl.com?postcode=SE19UD"); curl_exec($ch); Code (markup): Now this returns a the full address and lat/long. I need to extract these values seperatly so enter into a database so that number is inserted and so is road etc. How would i go about doing this? also i have to use curl as the server permits using fopen for url's. Cheers, Adam
$ch = curl_init("http://www.myurl.com?postcode=SE19UD"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $value = curl_exec($ch); echo $value; // Or do whatever else with it... PHP:
Thank you for your reply. I think i am mis-understanding what you have done here, So how would i extract just the postcode as i cannot seem to understand how it has done.
You need to add the returntransfer to be able to store the returned value in a variable, so you can work with it. If you want to extract data from the returned value, I'd need an example of the returned value.
Here is an example of the information given: Carlton CroftLeedsLS7 1HF17231Small User1LeedsLeeds CentralYorkshire430204347053.8077-1.54290113DAHFHyde Park and WoodhouseQ32Yorkshire and the HumberNorthY23FalseEY52StrugglingEmpty nests and seniorsSocial grade E, retired with experience of elementary occupations in manufacturing. Some do not use mail order but do look after grand-children What i need out of this is the name/number of the property, town/city, county and the longitude/latitude. If you could help with this that would be great. Cheers, Adam
This is going to be difficult as there are no separators or anything I could use to split the value. Is this the source code of the returned value, or that what your browser displays? If the source code is different, could you post this please?
I don't know how but i think that the service that i am using for postcode lookup and changed the way the information is outputted as it now comes out like so: <?xml version="1.0" ?> - <AFDPostcodeEverywhere> - <Address> <Organisation /> <Property /> <Street>Carlton Croft</Street> <Locality /> <Town>Leeds</Town> <County /> <Postcode>LS7 1HF</Postcode> <DPS /> <Mailsort>17231</Mailsort> <PostcodeType>Small User</PostcodeType> <HouseholdCount>1</HouseholdCount> <Authority>Leeds</Authority> <Constituency>Leeds Central</Constituency> <TVRegion>Yorkshire</TVRegion> <GridEast>43020</GridEast> <GridNorth>43470</GridNorth> <Latitude>53.8077</Latitude> <Longitude>-1.5429</Longitude> <STDCode>0113</STDCode> <WardCode>DAHF</WardCode> <WardName>Hyde Park and Woodhouse</WardName> <NHSCode>Q32</NHSCode> <NHSName>Yorkshire and the Humber</NHSName> <NHSRegion>North</NHSRegion> <NHSRegionCode>Y23</NHSRegionCode> <Changed>False</Changed> <CensusCode>EY52</CensusCode> <Affluence>Struggling</Affluence> <LifeStage>Empty nests and seniors</LifeStage> <AdditionalCensusInfo>Social grade E, retired with experience of elementary occupations in manufacturing. Some do not use mail order but do look after grand-children</AdditionalCensusInfo> </Address> </AFDPostcodeEverywhere> Code (markup): I assume this will be a lot easier to use. Cheers, Adam
EDIT: Nevermind, I didn't see you have to use cURL. This should do it: $ch = curl_init("http://www.myurl.com?postcode=SE19UD"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $value = curl_exec($ch); preg_match_all('/<(NHSName|NHSCode|Latitude|Longitude)>([^<]+)<\/\\1>/', $value, $matches); print_r($matches[2]); PHP:
That's great thanks, just one more thing how would i go about printing out a just the Latitude for example. I thought this print $matches['Town']; but that does not work.
You have to use the numeric index. echo $matches[2][0]; echo $matches[2][1]; echo $matches[2][2]; echo $matches[2][3]; PHP: Or: list($val1, $val2, $val3, $val4) = $matches[2]; echo $val1; echo $val2; // ... PHP: