Equivalent function for file_get_contents()

2019-02-09 18:15发布

I want to parse some information out of a html page. Currently I solve the problem like this:

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

$this->page is the url of the website. This works fine on XAMPP, but when I upload my script on my webserver, I get the following error message:

Warning: file_get_contents() [function.file-get-contents]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0

So obviously I am not allowed to execute that function on my webserver.

So is there an equivalent function to solve my problem?

5条回答
劳资没心,怎么记你
2楼-- · 2019-02-09 18:35

if you have curl, use it it is great for this.


            $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); 


查看更多
爷的心禁止访问
3楼-- · 2019-02-09 18:38

Use curl and why do you need to change the header to plain text to retrieve data? This is not necessary if you are retrieving data.

查看更多
别忘想泡老子
4楼-- · 2019-02-09 18:49

cURL is the usual standard solution.

查看更多
唯我独甜
5楼-- · 2019-02-09 18:50

Actually the function file_get_contents is not disabled,
but allow_url_fopen is disabled

you can replace it with 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);

However, if you server block outgoing traffic, curl does not help too

查看更多
来,给爷笑一个
6楼-- · 2019-02-09 18:50
登录 后发表回答