php: file_get_contents() work with CLI, but does not work when called on server (in page)

自古美人都是妖i 提交于 2020-01-29 16:44:05

问题


I am slightly baffled.

I am using the bit.ly PHP API to shorten some urls. This works fine on local host - but when I tried it on my server (php running in Apache), file_get_contents() returns an empty string.

I checked my apache logs and cannot see any warning/errors by Apache, so I tried the same command at the CL (using PHP CLI):

>php -a
php > $long_url = some_url_encoded_string;
php > echo file_get_contents($long_url);
{"errorCode": 0, "errorMessage": "", "results": {"http://www.example.html": {"userHash": "abc", "shortKeywordUrl": "", "hash": "xyz", "shortCNAMEUrl": "http://bit.ly/foobar", "shortUrl": "http://bit.ly/foobar"}}, "statusCode": "OK"}

Does anyone know what could be causing this behaviour?


回答1:


It probably has something to do with PHP configuration. Many servers have different php.inis for CLI and Apache. Check /etc/php5/ to see if there is more than one configuration. Specifically, you should be looking for allow_url_fopen.

EDIT: Actually, Ubuntu ships with that by default, the .ini is located at /etc/php5/apache2/php.ini



来源:https://stackoverflow.com/questions/6874857/php-file-get-contents-work-with-cli-but-does-not-work-when-called-on-server

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