如果我在浏览器中解析这个URL,它会给我XML,但当我把它放在我的代码中时,它会显示空数组
https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20csv%20where%20url%3D%22http%3A%2F%2Ffinance.yahoo.com%2Fd%2Fquotes.csv%3Fe%3D.csv%26f%3Dc4l1%26s%3DUSDINR%3DX%2CTRYINR%3DX%2CSARINR%3DX%2CEURINR%3DX%22&diagnostics=true
我的代码就像这样
public function fetch_Currency (){
$this->url="https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20csv%20where%20url%3D%22http%3A%2F%2Ffinance.yahoo.com%2Fd%2Fquotes.csv%3Fe%3D.csv%26f%3Dc4l1%26s%3DUSDINR%3DX%2CTRYINR%3DX%2CSARINR%3DX%2CEURINR%3DX%22&diagnostics=true";
$this->handle = file_get_contents($this->url);
if ($this->handle) {
$p = xml_parser_create();
xml_parse_into_struct($p, $this->result, $this->xml_val, $this->xml_index);
xml_parser_free($p);
}
}
public function get_exchange(){
echo "<pre>";
var_dump($this->xml_val);
echo "</pre>";
}
答案 0 :(得分:0)
这是在您的示例中解析XML的简单方法,并且可以访问元素:
$url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20csv%20where%20url%3D%22http%3A%2F%2Ffinance.yahoo.com%2Fd%2Fquotes.csv%3Fe%3D.csv%26f%3Dc4l1%26s%3DUSDINR%3DX%2CTRYINR%3DX%2CSARINR%3DX%2CEURINR%3DX%22&diagnostics=true";
$xml = simplexml_load_file($url);
//echo first currency
echo $xml->results->row[0]->col1;
//whole document tree
print_r($xml);