1
votes

I was using phpoffice template processing functionality with Laravel and it worked on Ubuntu, Windows and Mac. The functionality stopped working when I migrated to centos server. I figured out that the phpoffice was calling file_get_contents to open the template file and it is failing.

So started testing the file_get_contents function.

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

Error,

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 configuration,

allow_url_fopen = On // tried  allow_url_fopen = 1 also

Tried,

user_agent = "PHP"

I cannot change the function call to CURL approach as this is internally handled in the phpoffice composer package. Is there a solution for this?

I can access the file directly on the browser. No issues with that.

Edit:

  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');

Here 1 & 2 not working from the same server which the IP/domain points to but it works from any other systems including local.

Summary

Same issue with wget and CURL.

The server can't find itself using IP or domain but other systems can communicate with server.

The server identifies itself only as localhost.

1
are you using laravel /?Amit Sharma
@AmitSharma yesAnshad Vattapoyil
are you able to access this with a direct hit in the URL ?Amit Sharma
@AmitSharma yesAnshad Vattapoyil
Are there two servers involved here, or just one? That is, is the web page you're hitting on the same server as where the code is running (it seems so) or a different one?Greg Schmidt

1 Answers

0
votes

you can give it a try. hope it helps you out.

$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));

you can read out all the manual about the stramContext here on the php manual https://www.php.net/manual/en/function.file-get-contents.php