1

我在phpofficeLaravel 中使用模板处理功能,它在Ubuntu,WindowsMac. 当我迁移到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 composer 包中内部处理的。有解决方案吗?

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

编辑:

  1. echo file_get_contents('http://my-ip/api/public/storage/templates/template.docx');
  2. echo file_get_contents('http://my-domain/api/public/storage/templates/template.docx');
  3. echo file_get_contents('https://www.google.com/');
  4. echo file_get_contents('http://localhost/api/public/storage/templates/template.docx');

这里 1 和 2 不能在 IP/域指向的同一台服务器上工作,但它可以在包括本地在内的任何其他系统上工作。

概括

wget和相同的问题CURL

服务器无法使用 IP 或域找到自己,但其他系统可以与服务器通信。

服务器仅将自身标识为localhost

4

1 回答 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 的所有手册

于 2020-01-09T05:00:55.030 回答