2013-06-04 87 views
0

我想將下面的鏈接輸出保存到file.xml,但它不適用於我。它只是在另一個瀏覽器上顯示輸出。將瀏覽器輸出保存到php中的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); 
+0

'$ ch-> save'它是什麼? Curl_init返回句柄以使資源不是具有保存方法的對象。 – Robert

回答

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); 
+0

與此我能夠只顯示部分數據而不是整個數據。 –

+0

對不起,我不明白你的意思是「部分數據」。此代碼讀取整個頁面,而不是打印到標準輸出將其作爲字符串返回。沒有更多的變化。順便檢查我的答案更新,我添加了頁面檢索和保存完整的代碼片段。 – akhilless