首頁 php教程 php手册 php file_get_contents返回空 无效解决办法

php file_get_contents返回空 无效解决办法

May 24, 2016 pm 01:00 PM

file_get_contents函数多用来于来采集远程服务器上的内容,但使用file_get_contents函数之前我们在php.ini中是必须把allow_url_fopen开启才行

问题描述:

fopen(),file_get_contents(),getimagesize() 等都不能正常获得网络上的内容,具体表现为凡参数是URL的,一律返回空值.

如果是windows可找开 allow_url_fopen开启,如果是否linux中可以重新编译PHP,去掉-with-curlwrapper 参数——编译前记得先执行 make clean.

windows 在未开启allow_url_fopen时我们利用如下代码:

<?php 
$file_contents = file_get_contents("http://www.phprm.com/"); 
echo $file_contents;
登入後複製


是获取不到值的,但我们可以利用function_exists来判断此函数是否可用,代码如下:

<?php
function file_get_content($url) {
    if (function_exists(&#39;file_get_contents&#39;)) {
        $file_contents = @file_get_contents($url);
    }
    if ($file_contents == &#39;&#39;) {
        $ch = curl_init();
        $timeout = 30;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $file_contents = curl_exec($ch);
        curl_close($ch);
    }
    return $file_contents;
}
登入後複製


本文地址:

转载随意,但请附上文章地址:-)

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1662
14
CakePHP 教程
1419
52
Laravel 教程
1311
25
PHP教程
1261
29
C# 教程
1234
24