PHP: “file_get_contents()” returning NULL from content-verified URL

白昼怎懂夜的黑 提交于 2019-12-05 11:43:09

Start to debug this now:

# we want text output for debugging. Either this works or you need to look into
# source in your browser (or even better, debug with the `curl` commandline client.)
header('Content-Type: text/plain');

# For quick and dirty debugging - start to tell about if something is worth
# to warn or notice about. (do logging in production)
error_reporting(~0); ini_set('display_errors', 1);

$url = 'http://localhost/bbq/company/get/id/2131/return/json';
$results = file_get_contents($url);
var_dump($http_response_header, $results);

# hard exit, flush all (potential) buffers.
die();

A request to the file must now at least give you back some text output because of the var_dump. You can also add some marker output at the beginning below the header call, so it's clear that the script has been called and it did start to run.

However a better variant is using a step-debugger and creating a debugging session with breakpoints. I recommend Xdebug.

I can see this is an old post, but I had a similar issue today with file_get_contents() using a relative file path, returning an empty string.

I use UTF-8 encoding on all my files, and I found that I forgot to change the encoding on one file (which I couldn't retrive via file_get_contents() ). When I changed the encoding it "magically" worked.

Thre problem was coused by the non ANSI characters in an ANSI encoded file (eg.: á,é,ű,ő).

Hope this helps someone!

Check is it enabled in your php.ini

ini_get('allow_url_fopen')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!