PHP Warning: failed to open stream: HTTP request failed

17,755

You can add ignore_errors => true to your file_get_contents call in order to get the full message. That will ignore the error message but you would be able to get more information on what's going on. More information here (And don't forget to remove it after if you want it to break if there is another error.):

https://stackoverflow.com/questions/11475657/ignoring-errors-in-file-get-contents-http-wrapper/11479968#11479968

In my experience, this error is probably because your php doesn't have the root ca certificate in order to validate HTTPS calls. Adding the cacert.pem file to your instance with the configuration in php.ini would fix that issue.

https://stackoverflow.com/questions/41772340/how-do-i-add-a-certificate-authority-to-php-so-the-file-function-trusts-certif

In any case, first thing would be to try to get more information on your error since 400 is can mean a couple of things.

Share:
17,755
sebelk
Author by

sebelk

Updated on September 18, 2022

Comments

  • sebelk
    sebelk over 1 year

    I have two web servers:

    www.example.com.ar and eventos.example.com.ar

    I get the errors such as:

    [Wed Sep 16 12:11:30.872792 2020] [php7:warn] [pid 17125] [client xx.yy.226.234:48896] PHP Warning: file_get_contents(https://eventos.example.com.ar/eventos/api/events?month=9&year=1999): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request\r\n in /var/www/www_example_com_ar/wp-content/plugins/example-plugin/mlc-plugin.php on line 340

    The line 340th has:

    $rest = file_get_contents('https://eventos.example.com.ar/eventos/api/events?month='.$month.'&year='.$year, true);

    I've tested with curl on the same host that runs www.example.com.ar and I don't get such an error:

    curl -I "https://eventos.example.com.ar/eventos/api/events?month=9&year=1999"
    HTTP/1.1 200 OK
    Date: Wed, 16 Sep 2020 19:26:05 GMT
    Server: Apache/2.4.39 () OpenSSL/1.0.2k-fips
    X-DNS-Prefetch-Control: off
    X-Frame-Options: SAMEORIGIN
    Strict-Transport-Security: max-age=15552000; includeSubDomains
    X-Download-Options: noopen
    X-Content-Type-Options: nosniff
    X-XSS-Protection: 1; mode=block
    Access-Control-Allow-Origin: *
    Content-Type: application/json; charset=utf-8
    Content-Length: 37027
    ETag: W/"90a3-gzipB01lW17JrEb0GD0k0g2zFow"
    
    

    I have installed:

    • Apache: httpd-2.4.6-90.el7.centos.x86_64
    • PHP: php73-php-7.3.15-1.el7.remi.x86_64

    By the way, I have:

    php73 -i | grep allow_url_fopen
    allow_url_fopen => On => On
    

    On CentOS 7.7

    So, the question is: what does it cause that error?

    • Michael Hampton
      Michael Hampton over 3 years
      But what were $month and $year set to? This looks like it will end up being a bug in your program. We're not able to help with debugging PHP code, but you can ask about that on Stack Overflow.
    • sebelk
      sebelk over 3 years
      You're right, but using curl with the same url reported in the log, doesn't get the HTTP 400 status...
    • Michael Hampton
      Michael Hampton over 3 years
      You should check the logs of the API server.
    • dortegaoh
      dortegaoh over 3 years
      Is selinux configured to allow outgoing connections for Apache? (By default it is not)