Using curl to extract..

Discussion in 'PHP' started by adamjblakey, May 8, 2007.

  1. #1
    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
     
    adamjblakey, May 8, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    $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:
     
    nico_swd, May 8, 2007 IP
  3. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #3
    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.
     
    adamjblakey, May 9, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    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.
     
    nico_swd, May 9, 2007 IP
  5. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #5
    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
     
    adamjblakey, May 9, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    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?
     
    nico_swd, May 9, 2007 IP
  7. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #7
    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
     
    adamjblakey, May 9, 2007 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    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:
     
    nico_swd, May 9, 2007 IP
  9. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #9
    Only using php4 :(

    They won't upgrade to 5 either.
     
    adamjblakey, May 9, 2007 IP
  10. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #10
    I edited my post above. Give it a try.
     
    nico_swd, May 9, 2007 IP
  11. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #11
    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.
     
    adamjblakey, May 9, 2007 IP
  12. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #12
    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:
     
    nico_swd, May 9, 2007 IP
  13. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #13
    Thanks a lot nico for all your help :)
     
    adamjblakey, May 9, 2007 IP
  14. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #14
    You're welcome. :)
     
    nico_swd, May 9, 2007 IP
    decepti0n likes this.