file_get_contents在CentOS 7上不起作用

问题描述 投票:0回答:1

我在Laravel中使用phpoffice模板处理功能,并且可以在UbuntuWindowsMac上使用。当我迁移到centos服务器时,该功能停止工作。我发现phpoffice正在调用file_get_contents打开模板文件,但失败了。

因此开始测试file_get_contents函数。

echo file_get_contents('http://my-server-ip-address/api/public/storage/templates/template.docx');

错误,

ErrorException (E_WARNING)
file_get_contents(http://my-server-ip-address/api/public/storage/templates/template.docx): failed to open stream: Connection timed out

php.ini配置,

allow_url_fopen = On // tried  allow_url_fopen = 1 also

已尝试,

user_agent = "PHP"

我无法将函数调用更改为CURL方法,因为这是在phpoffice作曲程序包中内部处理的。有解决方案吗?

我可以直接在浏览器中访问文件。没问题。

编辑:

如果我将ip更改为localhost,则可以正常工作,

echo file_get_contents('http://localhost/api/public/storage/templates/template.docx');

但是我不能随处进行更改,因为URL来自db。

还可以与其他第三方URL一起使用,

echo file_get_contents('https://www.google.com/');
php laravel centos firewall phpoffice
1个回答
0
投票

您可以尝试一下。希望它可以帮助您。

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

 $arrContextOptions=array(
    'http' => array(
        'method'=>"GET",
        'header'=>"Content-Type: text/html; charset=utf-8"
    ) ,
    "ssl"=>array(
          "verify_peer"=>false,
          "verify_peer_name"=>false,
          "allow_self_signed" => true, // or "allow_self_signed" => false,

      ),
  );  

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

您可以在php手册https://www.php.net/manual/en/function.file-get-contents.php上阅读有关stramContext的所有手册

© www.soinside.com 2019 - 2024. All rights reserved.