2012-09-30 40 views
0

我使用雅虎的财务股票报价从他们的API获取股票行情数据。 要使用雅虎股票部件远程调用使用PHP

$data = file_get_contents("http://quote.yahoo.com/d/quotes.csv?s=appl&f=sl1d1t1c1ohgv&e=.csv"); 
$values = explode(",", $data); 
echo '<pre>'; 
print_r($values);  

获取数据现在这个工作在我的本地服务器(本地主机),即$值呼应了完美的罚款。 但是,当我上传这个文件到我的服务器上时,它打印出URL:http://quote.yahoo.com/d/quotes.csv?s=appl&f=sl1d1t1c1ohgv&e=.csv。我知道服务器上的file_get_contents存在一些问题。甚至allow_url_fopen在服务器上设置为'ON'。只是似乎无法找出服务器端的问题。

回答

0

您可能服务器设置有问题不让您使用file_get_contents()。在试图从其他域中获取内容时,PHP curl将成为您的朋友。

我刚刚发现这个真棒小片段:http://snipplr.com/view/4084

这是复制的file_get_contents()功能的功能,但是使用curl

function file_get_contents_curl($url) { 
    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser. 
    curl_setopt($ch, CURLOPT_URL, $url); 

    $data = curl_exec($ch); 
    curl_close($ch); 

    return $data; 
} 

你的新代码应该是这样的:

$data = file_get_contents_curl("http://quote.yahoo.com/d/quotes.csv?s=appl&f=sl1d1t1c1ohgv&e=.csv"); 
$values = explode(",", $data); 
echo '<pre>'; 
print_r($values);  
+0

试过这样做...... iam仍面临同样的问题。 – user1411837

相关问题