簡體   English   中英

file_get_contents 不適用於 CentOS 7

[英]file_get_contents not working on CentOS 7

我在phpoffice中使用了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 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/域指向的同一台服務器上工作,但它可以在包括本地在內的任何其他系統上工作。

概括

wgetCURL同樣的問題。

服務器無法發現自己​​使用 IP 或域,但其他系統可以與服務器通信。

服務器僅將自身標識為localhost

你可以試一試。 希望它能幫助你。

$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 的所有手冊

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM