2011-01-11 104 views
6

我想解析一些HTML頁面的信息。 目前我解決這個問題是這樣的:file_get_contents()的等效函數?

header("Content-type: text/plain");  
$this->pageSource = file_get_contents ($this->page); 
header("Content-type: text/html"); 

$this->page是網站的URL。 這工作得很好的XAMPP,但是當我上傳我的腳本在我的網絡服務器,我收到以下錯誤信息:

警告:file_get_contents()函數[function.file-GET-內容]:HTTP://封裝在服務器配置中被禁用allow_url_fopen = 0

所以顯然我不能在我的web服務器上執行該功能。

那麼是否有一個等價的函數來解決我的問題?

+3

你試過捲曲嗎? – 2011-01-11 09:35:51

+0

您可以使用該功能,但不能將URL與本地文件一起使用,這就是錯誤消息告訴您的。 – Tobias 2011-01-11 09:36:44

回答

19

其實功能file_get_contents沒有被禁用,
allow_url_fopen被禁用

你可以用curl

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $this->page); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$this->pageSource = curl_exec($ch); 
curl_close($ch); 

更換然而,如果你的服務器塊傳出流量,curl沒有幫助過

0

使用curl以及爲什麼需要將標題更改爲純文本以檢索數據?如果您正在檢索數據,這不是必需的。

0

如果你有捲曲,使用它對它很好。


      $urlx = 'http://yoururl'; 

      $data="from=$from&to=$to&body=".urlencode($body)."&url=$url"; 
//set post parameters 
      $process = curl_init($urlx); 
//init curl connection 
      curl_setopt($process, CURLOPT_HEADER, 0); 

      curl_setopt($process, CURLOPT_POSTFIELDS, $data); 

      curl_setopt($process, CURLOPT_POST, 1); 

      curl_setopt($process, CURLOPT_RETURNTRANSFER,1); 

      curl_setopt($process,CURLOPT_CONNECTTIMEOUT,1); 

      $resp = curl_exec($process); 
//your content 
      curl_close($process);