Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetching data from another website with php

Tags:

php

I need to fetch data from this web page central bank of armenia

In my html form user must insert the price of his supply. He/she selects the currency (USD, EUR or AMD) and types its value. After that I need to convert inserted price to other two currencies and add them to my database. So how can I get USD and EUR exchange rates from the site given above using PHP and attach them to variables.

like image 347
Rafael Sedrakyan Avatar asked Dec 13 '22 10:12

Rafael Sedrakyan


2 Answers

I normally don't do end-solutions in Q&A forums, but let it be, you challenged me :)

$content = file_get_contents("http://www.cba.am/am/SitePages/Default.aspx");

preg_match('#<b>USD</b>(.*)<em class="w_50">([0-9\.]*)</em><em class="w_50">([0-9\.]*)</em>#Uis', $content, $USDmatch);
preg_match('#<b>EUR</b>(.*)<em class="w_50">([0-9\.]*)</em><em class="w_50">([0-9\.]*)</em>#Uis', $content, $EURmatch);
preg_match('#<b>GBP</b>(.*)<em class="w_50">([0-9\.]*)</em><em class="w_50">([0-9\.]*)</em>#Uis', $content, $GBPmatch);

$eur = $EURmatch[3];
$usd = $USDmatch[3];
$gbp = $GBPmatch[3];

echo "EUR: $eur USD: $usd GBP: $gbp";

Let me, however, remind you that this kind of data fetching may be considered copyright infringement and abuse of Central bank of Armenia's servers.

Also, this is not a permanent solution, since bank may change it's website HTML structure anytime, breaking your code.

I would suggest using some kind of public API for that.

like image 108
Matej Balantič Avatar answered Jan 02 '23 21:01

Matej Balantič


I would recommend using cURL to actually make the call then DOM to parse. The advantage to using DOM is allowing you a little bit of leeway when it comes to changes on the website. The advantage to curl being extended flexibility and the ability to return errors. This will require quite a bit of studying on your part in order to determine the correct values to look for. The below code should get you started:

// Curl
function curl($url, $post=''){


        //cURL options
        $options = array(

            CURLOPT_RETURNTRANSFER => true,     // return web page
            CURLOPT_HEADER         => false,    // don't return headers
            CURLOPT_FOLLOWLOCATION => true,     // follow redirects
            CURLOPT_ENCODING       => "",       // handle all encodings
            CURLOPT_AUTOREFERER    => true,     // set referer on redirect
            CURLOPT_CONNECTTIMEOUT => 500,      // timeout on connect
            CURLOPT_TIMEOUT        => 500,      // timeout on response
            CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
            CURLOPT_SSL_VERIFYHOST => 0,
            CURLOPT_SSL_VERIFYPEER => 0,
            CURLOPT_USERAGENT      => "",
            CURLOPT_COOKIESESSION  => false,
            CURLOPT_COOKIEJAR     => $this->ckfile, // Cookies
            CURLOPT_COOKIEFILE     => $this->ckfile, //Cookies...yum


        );

        //Go go go!
        $ch      = curl_init( $url );
        curl_setopt_array( $ch, $options );

        $output['content'] = curl_exec( $ch );
        $output['err']     = curl_errno( $ch );
        $output['errmsg']  = curl_error( $ch );
        $output['header']  = curl_getinfo( $ch );
        return $output;

    }

Once you have $output, you can parse with DOM. Few ways you can go about it, I recommend XPATH queries

//Create DOM
        $doc = new DOMDocument;
        @$doc->loadHTML($curl['content']);
        $doc->preserveWhiteSpace = false;

$xpath = new DOMXpath($doc);

        $nodeList = $xpath->query("//div[@id='test']"); // I would recommend trying to find an element that contains the currency elements, but has an attribute that will most likely not be changed. IDs work well, sometime div classes also work. Check out http://www.exampledepot.com/egs/org.w3c.dom/xpath_GetElemByText.html

        foreach($nodeList as $node){

            // From here you would want to select the element http://php.net/manual/en/domdocument.getelementsbytagname.php

}

From there you return it. I would instead recommend parsing http://themoneyconverter.com/RSSFeeds.aspx

like image 22
Dave Lasley Avatar answered Jan 02 '23 21:01

Dave Lasley