2011-05-09 77 views
0

好的,對於一個項目,有一個XML頁面,它使用用戶的Cookie來生成輸出。包括XML文件和設置Cookie

我沒有訪問此頁面,但希望使用PHP從它提取信息。

到目前爲止,我的想法是某種PHP包括將cookie設置爲外部包含的文件。

任何指針或建議將是偉大的。謝謝。

編輯:我無法控制XML頁面託管的域。

回答

0

你可以使用捲曲,包括cookie罐:

http://curl.haxx.se/libcurl/php/examples/cookiejar.html

<?php 
/* 
This script is an example of using curl in php to log into on one page and 
then get another page passing all cookies from the first page along with you. 
If this script was a bit more advanced it might trick the server into 
thinking its netscape and even pass a fake referer, yo look like it surfed 
from a local page. 
*/ 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookieFileName"); 
curl_setopt($ch, CURLOPT_URL,"http://www.myterminal.com/checkpwd.asp"); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, "UserID=username&password=passwd"); 

ob_start();  // prevent any output 
curl_exec ($ch); // execute the curl command 
ob_end_clean(); // stop preventing output 

curl_close ($ch); 
unset($ch); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookieFileName"); 
curl_setopt($ch, CURLOPT_URL,"http://www.myterminal.com/list.asp"); 

$buf2 = curl_exec ($ch); 

curl_close ($ch); 

echo "<PRE>".htmlentities($buf2); 
?> 
+0

這看起來不錯,我相信第一卷曲會話的(不必要的)登錄,第二個實際的文件觀看? – JakeSteam 2011-05-10 07:40:32

+0

檢出了文檔,不知道這是否可行,因爲我無法控制外部服務器,所以無法設置Cookie。如果我設計了我自己的腳本,那麼這些cookie不會對它有效。 – JakeSteam 2011-05-10 07:53:13

+0

@Jake對不起,剛纔看到你的評論。你是對的。第一個捲曲文章是你允許服務器設置你需要的cookies的地方。可以認爲它是自動提交登錄表單。獲得cookie後,您可以使用其他帖子獲取所需的數據。這只是一個自動瀏覽器訪問。 – 2011-05-10 15:56:57