file_get_contents - failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found

前端 未结 4 1179
迷失自我
迷失自我 2020-11-27 20:46

I\'m having some weird problems with file_get_contents after moving my site to a new domain. I had to set up a new domain and IP address (using Plesk) to get a new ssl certi

相关标签:
4条回答
  • 2020-11-27 21:27

    If you just need to handle the warning when the URL is not found (as I did), you may just do this to turn Warnings into Exceptions:

    set_error_handler(
      function ($err_severity, $err_msg, $err_file, $err_line, array $err_context) {
        // do not throw an exception if the @-operator is used (suppress)
        if (error_reporting() === 0) return false;
    
        throw new ErrorException( $err_msg, 0, $err_severity, $err_file, $err_line );
      },
      E_WARNING
    );
    try {
      $contents = file_get_contents($your_url);
    } catch (Exception $e) {
      echo $e->getMessage();
    }
    restore_error_handler();
    

    Solution based on this thread/question.

    0 讨论(0)
  • 2020-11-27 21:33

    Most hosting provides now block the furl_open parameter which allows you to use file_get_contents() to load data from an external url.

    You can use CURL or a PHP client library like Guzzle

    0 讨论(0)
  • 2020-11-27 21:34

    I've had this problem too, when I working on a little test server at home. The domain name is resolved to your external IP address, and a request is sent. But because the request is coming from inside your network, the router doesn't recognise it as a normal request. It probably has a web interface for configuring it, and tries to return a page from its own management system, which is then not found at the path you specified.

    In that case, I was working on a Windows PC, and I could solve it by adding the domain I was testing to my hosts file, specifying 127.0.0.1 as the IP-address (or the IP-address of the server, if it is another machine within the same network). In Linux there should be a similar solution, I think.

    The problem isn't PHP or your server, but your router.

    0 讨论(0)
  • 2020-11-27 21:43

    Try to do this :

    file_get_contents('https://mydomain.com?'.urlencode('limit=4&offset=0&s_date=2012-02-05&e_date=2012-03-13&order=release_date&dir=desc&cid=12'));
    
    0 讨论(0)
提交回复
热议问题