2011-01-11 44 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);