0

我想将以下链接的输出保存到 file.xml 但它不适合我。它只是在另一个浏览器上显示输出。

$url = 'http://www.forexwire.com/feed/full?username=alumfx&password=T7M9Exb4';
$fp = fopen (dirname(__FILE__). '/file.xml', 'w+');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
$ch->save('file.xml');
fclose($fp);
4

1 回答 1

0

默认情况下,CURL 的 exec 函数将结果作为标准输出返回。您需要添加它以使其以字符串形式返回结果:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

然后将其保存到变量中

$output = curl_exec($ch);
//do whatever with the $output

这样完整的代码片段可以如下所示:

$ch = curl_init('http://www.forexwire.com/feed/full?username=alumfx&password=T7M9Exb4'); 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
$output = curl_exec($ch); 
curl_close($ch);
file_put_contents('path/to/file', $output);
于 2013-06-04T09:39:35.450 回答