php file_get_contents() 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27613432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:26:12  来源:igfitidea点击:

file_get_contents( ) not working

phpapachecurl

提问by Kamal Saleh

I have a script that uses file_get_contents()to get json response from remote server . while file_get_contents()is working properly on local files but not working with http or https it gives me the following error file_get_contents(): http:// wrapper is disabled in the server configuration by allow_url_fopen=0 inand file_get_contents(https://api.domain.com/resolve.json?url=blablabla): failed to open stream: no suitable wrapper could be found.. it is a dedicated server and I have WHM .. I've tried

我有一个脚本,用于file_get_contents()从远程服务器获取 json 响应。虽然file_get_contents()在本地文件上正常工作但不能使用 http 或 https 它给了我以下错误file_get_contents(): http:// wrapper is disabled in the server configuration by allow_url_fopen=0 infile_get_contents(https://api.domain.com/resolve.json?url=blablabla): failed to open stream: no suitable wrapper could be found..它是一个专用服务器,我有 WHM ..我试过

  1. setting allow_url_fopen = onon WHM PHP configuration Editor but This didn't work.
  2. creating a php.ini file with allow_url_fopen = onin the directory where the error occurred but this didn't work.
  3. added ini_set('allow_url_fopen', 'On');to the start of the PHP script but this didn't work .
  1. allow_url_fopen = onWHM PHP 配置编辑器上的设置,但这不起作用。
  2. allow_url_fopen = on在发生错误的目录中创建一个 php.ini 文件,但这不起作用。
  3. 添加ini_set('allow_url_fopen', 'On');到 PHP 脚本的开头,但这不起作用。

I know I can use Curl instead but I want to know why this is not working .. the script is working properly on other server and localhost

我知道我可以使用 Curl 代替,但我想知道为什么这不起作用..脚本在其他服务器和本地主机上正常工作

update :

更新 :

phpinfo();

gives me

给我

allow_url_fopen Off 
allow_url_include   Off 
always_populate_raw_post_data   Off

that means my php.ini changes didn't take effect .. what I'm doing wrong ?

这意味着我的 php.ini 更改没有生效..我做错了什么?

回答by Dushyant Dagar

$url= 'https://example.com';

$arrContextOptions=array(
      "ssl"=>array(
            "verify_peer"=>false,
            "verify_peer_name"=>false,
        ),
    );  

$response = file_get_contents($url, false, stream_context_create($arrContextOptions));

This will allow you to get the content from the url whether it is a HTTPS, no need to change in php ini.

这将允许您从 url 获取内容,无论它是 HTTPS,无需在 php ini 中更改。

回答by baao

Login to your server via ssh and type

通过 ssh 登录到您的服务器并键入

sudo nano /etc/php5/apache2/php.ini //<<<< ubuntu/debian server, might differ for you

in the file, simply press "ctrl + w"and type "allow_url_fopen"and Return, most probably you will come to the explanation first, so repeat the search a couple of times. Now you can change the entry from

在文件中,只需按"ctrl + w"并键入"allow_url_fopen"并返回,很可能您会首先看到解释,因此请重复搜索几次。现在您可以更改条目

allow_url_fopen=0

allow_url_fopen=0

to

allow_url_fopen=1

allow_url_fopen=1

press "ctrl + x"and confirm the file save with "y".

"ctrl + x"并用 确认文件保存"y"

Then type

然后输入

sudo service apache2 restart

This will restart apache so the new php.ini configuration can be loaded. After those steps, you should be able to use file_get_contentsexternally.

这将重新启动 apache,以便可以加载新的 php.ini 配置。完成这些步骤后,您应该可以在file_get_contents外部使用。



SIDENOTE

边注

If you can't find your php.ini file, you will find the path to the loaded php.ini file in the top section of your phpinfo()

如果你找不到你的 php.ini 文件,你会在你的文件的顶部找到加载的 php.ini 文件的路径 phpinfo()

enter image description here

在此处输入图片说明

回答by Pham Lai

If you have root access, edit php.ini, usually located at /etc/php.ini.

如果您有 root 访问权限,请编辑php.ini,通常位于/etc/php.ini.

If you dont have root access, try to add ini_set('allow_url_fopen', 'On');or ini_set('allow_url_fopen', '1');.

如果您没有 root 访问权限,请尝试添加ini_set('allow_url_fopen', 'On');ini_set('allow_url_fopen', '1');

If you can't see php.ini, try using phpinfo()in a PHP script to find the php.inilocation.

如果看不到php.ini,请尝试phpinfo()在 PHP 脚本中使用来查找php.ini位置。

回答by Safeer Ahmed

Use curl as an alternative of file_get_contents( ) and this is the function I am using

使用 curl 作为 file_get_contents( ) 的替代方案,这是我正在使用的函数

function url_get_contents ($Url) {
    if (!function_exists('curl_init')){ 
        die('CURL is not installed!');
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $Url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

回答by Sahil Jangra

$id = $_GET['id'] ;
$api_url = "http://localhost/../API.php?id='$id' ";
$port = 80;
$ch = curl_init( );
curl_setopt ( $ch, CURLOPT_URL, $api_url );
curl_setopt ( $ch, CURLOPT_PORT, $port );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
// Allowing cUrl funtions 20 second to execute
curl_setopt ( $ch, CURLOPT_TIMEOUT, 5 );
// Waiting 20 seconds while trying to connect
curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 5 );                                 
$response_string = curl_exec( $ch );

echo $response_string;

This is the best way to solve the web request problems

这是解决web请求问题的最好方法