我正在尝试使用PHP从URL获取xml内容,但我尝试的所有方法都返回空字符串或错误。该网址为:http://www.bmfbovespa.com.br/Pregao-Online/ExecutaAcaoAjax.asp?CodigoPapel=NATU3|PETR4|ITSA4|
首先,我尝试使用simplexml_load_file,并收到错误消息。然后我尝试使用file_get_contents,但是返回空。
最后,我使用cURL,使用以下命令:
$ch=curl_init();
$timeout=5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
// Get URL content
$lines_string=curl_exec($ch);
// close handle to release resources
curl_close($ch);
但也没有任何回复。有人可以帮帮我吗?
答案 0 :(得分:1)
也许你没有在程序的其余部分正确使用$lines_string
因为这只是起作用:
$ch=curl_init();
$timeout=5;
$url = 'http://www.bmfbovespa.com.br/Pregao-Online/ExecutaAcaoAjax.asp?CodigoPapel=NATU3|PETR4|ITSA4|';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$lines_string=curl_exec($ch);
print_r($lines_string);
curl_close($ch);
并返回:
<?xml version="1.0"?>
<ComportamentoPapeis><Papel Codigo="NATU3" Nome="NATURA ON NM" Ibovespa="#" Data="19/12/2014 16:47:14" Abertura="31,00" Minimo="30,74" Maximo="31,44" Medio="31,06" Ultimo="30,88" Oscilacao="-0,12"/><Papel Codigo="PETR4" Nome="PETROBRAS PN" Ibovespa="#" Data="19/12/2014 16:47:49" Abertura="9,94" Minimo="9,57" Maximo="9,98" Medio="9,72" Ultimo="9,75" Oscilacao="3,06"/><Papel Codigo="ITSA4" Nome="ITAUSA PN N1" Ibovespa="#" Data="19/12/2014 16:47:47" Abertura="9,53" Minimo="9,42" Maximo="9,63" Medio="9,55" Ultimo="9,52" Oscilacao="1,27"/></ComportamentoPapeis>