2013-10-08 29 views
0

我打算從頁面獲取內容。 我用file_get_contents()來獲取內容,但失敗了。瀏覽器沒有顯示任何內容。如何從其網址調用api與PHP的頁面獲取內容

$content = file_get_contents("http://localhost/mediawiki/api.php?action=query&titles=firstPage&format=xml&prop=info&inprop=watched"); 

實際上,上面的鏈接可以顯示關於xml中firsrPage頁面的信息。

如何從調用api的url獲取內容?

+0

當您嘗試手動訪問網址,您的瀏覽器,會發生什麼? –

+0

沒有顯示。 – asdk77

+0

上面的鏈接可以顯示一個xml文件,其中包含有關稱爲firstPage的頁面的信息。我想獲取信息。謝謝! – asdk77

回答

0

這裏您可以使用CURL從API鏈接獲取元素,使用CURL爲您提供更多選項以從文件中獲取元素,還可以使用帶有上下文()的file_get_contents(),從而獲得鏈接中的元素,但我認爲捲曲。

$url = 'API_URL' 

$ch=curl_init(); 
curl_setopt($ch,CURLOPT_URL,$url); 
curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE); 

$result=curl_execute($ch); 
if(!$result){ 
exit ('cURL ERROR: '.curl_error($ch)); 
} 
var_dump($result); 

否則你可以試試這個:

// Dump contents (without tags) from HTML 
echo file_get_html('your url')->plaintext;