View Full Version : Using curl to extract..
adamjblakey
May 8th 2007, 2:07 am
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);
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
nico_swd
May 8th 2007, 2:14 am
$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...
adamjblakey
May 9th 2007, 1:18 am
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.
nico_swd
May 9th 2007, 1:23 am
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.
adamjblakey
May 9th 2007, 7:53 am
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
nico_swd
May 9th 2007, 8:08 am
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?
adamjblakey
May 9th 2007, 8:16 am
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>
I assume this will be a lot easier to use.
Cheers,
Adam
nico_swd
May 9th 2007, 8:27 am
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]);
adamjblakey
May 9th 2007, 8:40 am
Only using php4 :(
They won't upgrade to 5 either.
nico_swd
May 9th 2007, 8:50 am
I edited my post above. Give it a try.
adamjblakey
May 9th 2007, 9:10 am
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.
nico_swd
May 9th 2007, 9:13 am
You have to use the numeric index.
echo $matches[2][0];
echo $matches[2][1];
echo $matches[2][2];
echo $matches[2][3];
Or:
list($val1, $val2, $val3, $val4) = $matches[2];
echo $val1;
echo $val2;
// ...
adamjblakey
May 9th 2007, 9:16 am
Thanks a lot nico for all your help :)
nico_swd
May 9th 2007, 9:17 am
You're welcome. :)
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.