什么是解析雅虎货币的正确方法http://finance.yahoo.com/connection/currency-converter-cache?date?

时间:2015-10-12 14:51:03

标签: php json parsing yahoo-api yahoo-finance

由于我尝试过的代码并通过试用删除来获取返回的json内容如下 我使用的方法。

$(event.target).find('youtube-player').data('YoutubeAPI')

代码以yahoo和json格式返回给定的批量

删除未知格式

$date= YYYYMMDD; 

//example '20140113'

$handle = fopen('http://finance.yahoo.com/connection/currency-converter-cache?date='.$date.'', 'r');

//sample code is http://finance.yahoo.com/connection/currency-converter-cache?date=20140208 paste the url in browser;

// use loop to get all until end of content 

   while (!feof($handle)) {
            $contents .= fread($handle, 8192);
        }
        fclose($handle);

通过

   "/**/YAHOO.Finance.CurrencyConverter.addConversionRates (" and ends with ");"

1 个答案:

答案 0 :(得分:3)

我更喜欢使用file_get_contentshtmlpreg_match_all清除json,即:

<?php
$json = file_get_contents("http://finance.yahoo.com/connection/currency-converter-cache?date=20140113");
preg_match_all('/\((.*)\);/si', $json, $json, PREG_PATTERN_ORDER);
$json =  $json[1][0];
$json = json_decode($json,true);

foreach ($json["list"]["resources"] as $resource){
    echo $resource["resource"]["fields"]["date"];
    echo $resource["resource"]["fields"]["price"];
    echo $resource["resource"]["fields"]["symbol"];
    echo $resource["resource"]["fields"]["price"];
}

注意:

我已经测试了代码,它按预期工作。