How to fetch / extract data using CURL in PHP?

This is an example that fetches / extracts the information that we get while we search in google as define:words.

Open google.com and type define:zany in search box and press enter.

You will get list for definition of word zany.

In this example, using curl i am demonstrating to fetch the data from that page starting with <ul> tag to </ul> tag.

<?php
$url = "http://www.google.com/search?q=define:zany";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$var= curl_exec($ch);
curl_close($ch);
$first= stripos($var,'<ul’,0) ;
$second= stripos($var,'</ul>’,0) ;
echo substr($var,$first,$second-$first);
?>

You may also like...

18 Responses

  1. Many thanks for providing this post. It is really essential for me.

  2. Tessa says:

    I’m not easily ipmrsesed. . . but that’s impressing me! 🙂

  3. Wedding planner says:

    Great article thanks

  4. chand says:

    hi,
    I need some help like i have api of one service provider and some tracking id’s. they are providing data in xml, and when i am passing id’s by using html text tag and posting to curl it is not coming but id is displaying on url. when i pass id directly it is comming. what to do.

  5. admin says:

    Hello chand , thanks for your queries. Please recheck your codes or test by making similar example in your local host. Without seeing actual scenario i cannot help…

  6. admin says:

    Hello Chand, the code below will give you value of first Current Status found on page http://trackntrace.aflwiz.com/aflwizhtmltrack?shpntnum=84252622306407166 . Hope this helps.

  7. chand says:

    2011-08-24 16:12

    how to take only date from this in php

  8. admin says:

    Hello friend chand, Please send your question clearly in my email mast.ratna@gmai.com so that i can find exact solution and help you. Thanks

  9. chand says:

    Thanks a lot for your help.

  10. Kumar says:

    How to fetch data in php(curl) from this site http://www.metrorailindia.com 1> i want train fare from one station to another 2> timings of train in current station 3> stations comes between one station to another

  11. Gomathi says:

    Good article and informative… keep it rock

  12. ruchit says:

    i want to extract and display data(textual and binary data) from a website that provides informations about different cars falling under a specified range of price….can you help me?
    i just want to use these urls in my database and search for the required data by crawling through these websites.

  13. sujitha says:

    thank u .it helps me alot

  14. Sachin says:

    Thanks a lot, much needed info. keep it up.

  15. Utpal says:

    Impressive info. Thanks for this kind help.

  16. gaurav singh says:

    Very useful article thank you so much

  1. April 18, 2011

    […] highlighter works fine with Firefox but not working in ie Eg. http://rat32.com/rat32/?p=5 How to fix this problem? This entry was posted in Uncategorized. Bookmark the permalink. […]

Leave a Reply

Your email address will not be published. Required fields are marked *

*