2014-02-27 30 views
6

我想將頁面的文本複製到PHP變量中。 我用file_get_contents(...)方法,但它不爲我的地址 例如我的地址是工作:如何使用PHP中的代碼複製網頁的文本

http://www.bankmellat.ir/3/Default/94/1/Default/2/875/1/201.aspx?itemid=201

但是這個代碼不顯示此頁的文本:

$filename='http://www.bankmellat.ir/3/Default/94/1/Default/2/875/1/201.aspx?itemid=201'; 
$homepage = file_get_contents($filename , false); 
echo ($homepage); 
+0

你會得到什麼輸出?任何錯誤? – trizz

+0

嘗試添加

+0

Rohit Awasthi:沒用! – Sadegh

回答

0

大多數託管現在提供阻止furl_open參數,它允許您使用file_get_contents()從外部URL加載數據。

使用此代碼:

<?php 
$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1"; 

// INIT CURL 
$ch = curl_init(); 

//init curl 
curl_setopt($ch, CURLOPT_USERAGENT, $useragent); 

// SET URL FOR THE POST FORM LOGIN 
curl_setopt($ch, CURLOPT_URL, 'http://www.bankmellat.ir/3/Default/94/1/Default/2/875/1/201.aspx?itemid=201'); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); 

// common name and also verify that it matches the hostname provided) 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

// Optional: Return the result instead of printing it 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

// ENABLE HTTP POST 
curl_setopt ($ch, CURLOPT_POST, 1); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 

$store = curl_exec ($ch); 
echo $store; 

// CLOSE CURL 
curl_close ($ch); 
?> 

享受!

+0

我應該使用此代碼而不是URL?它錯誤,CURL是一個未定義的函數! – Sadegh

+0

@Sadegh試試這個代碼 – codemania

+0

非常感謝你;) 這段代碼在服務器上運行良好,但它在localhost中的「curl_init()」方法上出錯。 你有什麼想法嗎? – Sadegh

相關問題