在php中保存浏览器输出到xml文件


Save browser output to xml file in php

我想将下面链接的输出保存到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);

默认情况下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);