2011-08-22 94 views
2

我需要从这个网页central bank of armenia获取数据使用PHP

获取数据在我的HTML表单,用户必须将他的价格供应。他/她选择货币(美元,欧元或AMD)并输入其价值。之后,我需要将插入的价格转换为其他两种货币并将它们添加到我的数据库中。那么,如何使用PHP从上述网站获得美元和欧元汇率,并将它们附加到变量中。

+1

http://php.net/manual/en/book.curl.php –

回答

7

我一般不做Q中&一个论坛的最终解决方案,但希望这是你向我挑战:)

$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"; 

然而,让我提醒你,这样的数据取的可考虑BBE侵犯版权和滥用服务器亚美尼亚中央银行的服务器。

此外,这不是一个永久的解决方案,因为银行可能随时更改其网站HTML结构,破坏您的代码。

我会建议使用某种公共API。

+0

非常感谢您的帮助。我已经使用file_get_contents,str_pos和substr_functions,但你的解决方案更好:)我也理解更改cba.am html结构的问题。我正在旅游门户网站上工作,客户whants货币将从cba.am :)采取:)至于我,我会让它只有一种货币:) –

0

this class中有类似的功能,可以从意大利银行做汇率。意大利银行有一个公共链接,每天给你汇率。也许亚美尼亚银行也会给汇率提供一个公共链接,使用它(如果有的话),所以你不必做太复杂的解析,也没有任何版权问题。

+0

谢谢。我会寻找它。 –

3

我会建议使用cURL来实际进行调用,然后解析DOM。使用DOM的好处是让您在网站上进行更改时有一点余地。卷曲的优点是扩展灵活性和返回错误的能力。这需要您进行相当多的研究,以确定要查找的正确值。下面的代码应该让你开始:

// 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; 

    } 

一旦你有$输出,你可以用DOM解析。几种方法可以解决它,我推荐XPATH查询

//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 

} 

从那里你回报它。我会推荐解析http://themoneyconverter.com/RSSFeeds.aspx

+1

感谢您的回复。我猜cURL是相当强大的工具,但不幸的是我现在没有时间学习。工作必须尽快完成。我将来肯定会学到它。 –

+0

学习不是用于cURL,而是你试图解析的页面。如果你计划建立一个可靠的系统,你需要完全理解你试图解析的页面,以便获得可靠的数据。 –