Jump to content

Getting specific info from antother site


Bman900

Recommended Posts

In my site I would need to display currency rates, I am thinking of getting such info from an official page but can I do this automatically somehow with PHP? Meaning the info is somewhere on the page and I want to somehow grab that little piece of information.

 

I was thinking maybe I need a spider, and if so can any one point me to a good tutorial to get started?

Link to comment
Share on other sites

you can use file_get_contents or the cURL library to retrieve the contents of a page.  Then you would use a DOM parser or regex functions (like preg_match_all) to scrape the values off the page. 

 

WARNING: You should always get permission from the site owner before scraping their content.  People do not take kindly to others scraping their content and doing so has adverse effects.  You may have your IP blocked or you could get sued, depending on lots of factors. 

 

I know there are a lot of sites that offer ability to grab current currency rates through a feed or an api.  You will more than likely have to pay for such service, depending on how current you want the information to be, though.

Link to comment
Share on other sites

Alright I went with the XML route and found a bank that does this and I got it to read partially.

 

<Rate currency="AED">1.0338</Rate>  I can not get the rate out from between the brackets. Here is the code am using:

 

<?php

    $XML=simplexml_load_file("http://www.bnr.ro/files/xml/curs_2012_8_3.xml");

            
    foreach($XML->Body->Cube->Rate as $rate){
        //Output the value of 1EUR for a currency code
        echo '1 LEI ='.$rate["rate"].' '.$rate["currency"].'<br/>';
        //--------------------------------------------------
        //Here you can add your code for inserting
        //$rate["rate"] and $rate["currency"] into your database
        //--------------------------------------------------
    }
?>

 

I will only print the AED part.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.