Fetching data from other site with cURL

Discussion in 'PHP' started by Dondon2d, Apr 23, 2008.

  1. #1
    Hi guys, I need a little help. I want to fetch data from another site using cURL, but I don't know how to do it using preg_match_all and how to provide output.

    This is the source code of the data that I want to get:

    <td> <b>Text</b> </td>
    <td> <b>Numbers here</b> </td>
    <td> <b>Numbers here</b> </td>
    Code (markup):
    Thanks.
     
    Dondon2d, Apr 23, 2008 IP
  2. Louis11

    Louis11 Active Member

    Messages:
    783
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    70
    #2
    Here's a quick little cURL script I threw together for you:
    
    <?
    // call this function, and pass it the website URL
    // It will return a string with the websites source
    
    function curl_page($url){
    // create a new curl resource
    $ch = curl_init();
    			
    // set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    			
    // grab URL, and return output
    $output = curl_exec($ch);
    						
    // close curl resource, and free up system resources
    curl_close($ch);
    
    return $output;
    }
    ?>
    
    PHP:
    Alright, this is off the top of my head (and I always screw up the regex when I do it without testing). But this *should* work . . . if it doesn't just tinker with the regex part (the "/<td>.*<\/td>/" part).

    
    preg_match_all("/<td>.*<\/td>/", $data, $matches);
    var_dump($matches);
    
    PHP:
    Or something like that :)
    - Louis
     
    Louis11, Apr 23, 2008 IP
  3. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #3
    Little modification.

    
    preg_match_all("/<td>(.*)?<\/td>/iU", $data, $matches);
    var_dump($matches);
    
    PHP:
     
    xrvel, Apr 23, 2008 IP
  4. nandanamnidheesh

    nandanamnidheesh Active Member

    Messages:
    376
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    55
    #4
    tx me also searching for this ..
     
    nandanamnidheesh, Apr 24, 2008 IP
  5. Dondon2d

    Dondon2d Peon

    Messages:
    3,193
    Likes Received:
    146
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks Louis11 and xrvel, you guys brought the thorn out of me :D
     
    Dondon2d, Apr 24, 2008 IP
  6. Louis11

    Louis11 Active Member

    Messages:
    783
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    70
    #6
    No problem :) Feel free to PM me any time with any questions you have.
     
    Louis11, Apr 24, 2008 IP
  7. Dondon2d

    Dondon2d Peon

    Messages:
    3,193
    Likes Received:
    146
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I have some in mind, will PM you later :D
     
    Dondon2d, Apr 24, 2008 IP
  8. phpl33t

    phpl33t Banned

    Messages:
    456
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Very nice, Louis.
     
    phpl33t, Apr 24, 2008 IP
  9. Dondon2d

    Dondon2d Peon

    Messages:
    3,193
    Likes Received:
    146
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Very nice but didn't work out well, actually. Problems with the regexp I think.
     
    Dondon2d, Apr 24, 2008 IP
  10. decepti0n

    decepti0n Peon

    Messages:
    519
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Try using the DOM if you have php5

    
    $html = $curl_response; // whatever you get from the above script, the html of the page
    
    $dom = new DomDocument();
    @$dom->loadHTML($html);
    $tags = $dom->getElementsByTagName('td');
    foreach ($tags as $tag) {
        if ($tag->firstChild->tagName == 'b')
            echo $tag->textContent;
    }
    
    
    PHP:
     
    decepti0n, Apr 24, 2008 IP
  11. Dondon2d

    Dondon2d Peon

    Messages:
    3,193
    Likes Received:
    146
    Best Answers:
    0
    Trophy Points:
    0
    #11
    It was solved already. Thanks to xrvel, and I explored it a lil bit. Thanks for your responses though :)
     
    Dondon2d, Apr 24, 2008 IP