2010-09-01 56 views
0

我有一個PHP腳本,它使用'echo'命令和數據庫中的數據生成XML。 因此,如果我從瀏覽器訪問此腳本,我可以看到xml數據並下載它。如何使用WebClient下載PHP腳本生成的XML內容?

所以我寫一個軟件來檢索這個數據使用webclient類。但webclient只下載一個空文件,所以我想它試圖下載.php文件,而不是生成的動態內容。

PHP腳本發送header("Content-type: text/xml"),Web客戶端嘗試從https://mySecureServer.com/db/getXMLData.php下載(也許這是問題)。

任何想法?

編輯:Web客戶端代碼(只是撕開了一些本地的文件操作):

string url = @"https://mySecureServer.com/db/getXMLData.php"; 
WebClient client = new WebClient(); 
client.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCompletedEvtHdl); 
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressChangedEvtHdl); 
client.BaseAddress = @"https://mySecureServer.com/db/"; 
client.DownloadFileAsync(new Uri(url), toSavePath + filename); 
+0

Webclient無法下載.php文件,因爲這是永遠不可用的。 – 2010-09-01 17:07:29

+0

請顯示您正在使用的代碼下載。 – 2010-09-01 17:08:07

回答

0

如果你只是想下載此getXMLData.php腳本生成的XML數據,那麼你可以直接使用cURL得到它的內容:

function get_web_page($url) 
{ 
    $options = array(
     CURLOPT_RETURNTRANSFER => true,  // return web page 
     CURLOPT_HEADER   => false, // don't return headers 
     CURLOPT_FOLLOWLOCATION => true,  // follow redirects 
     CURLOPT_ENCODING  => "",  // handle all encodings 
     CURLOPT_AUTOREFERER => true,  // set referer on redirect 
     CURLOPT_CONNECTTIMEOUT => 120,  // timeout on connect 
     CURLOPT_TIMEOUT  => 120,  // timeout on response 
     CURLOPT_MAXREDIRS  => 10,  // stop after 10 redirects 
     CURLOPT_SSL_VERIFYPEER => false  // disable certificate checking 
    ); 

    $ch  = curl_init($url); 
    curl_setopt_array($ch, $options); 
    $content = curl_exec($ch); 
    $err  = curl_errno($ch); 
    $errmsg = curl_error($ch); 
    $header = curl_getinfo($ch); 
    curl_close($ch); 

    $header['errno'] = $err; 
    $header['errmsg'] = $errmsg; 
    $header['content'] = $content; 
    return $header; 
} 

//Now get the webpage 
$data = get_web_page("https://mySecureServer.com/db/getXMLData.php"); 

//Display the data (optional) 
echo "<pre>" . $data['content'] . "</pre>"; 
+0

嘗試了你的代碼,但得到了這樣的結果:**「指定的CGI應用程序由於沒有返回一組完整的HTTP頭而導致錯誤,它返回的頭是:」**,這似乎是IIS + PHP http://bugs.php.net/bug.php?id=25863)。你有任何其他的想法或者解決這個問題嗎? – Metraton 2010-09-03 10:46:02