php:file_get_contents()可与CLI一起使用,但在服务器上调用时不起作用(在页面中) [英] php: file_get_contents() work with CLI, but does not work when called on server (in page)

查看:107
本文介绍了php:file_get_contents()可与CLI一起使用,但在服务器上调用时不起作用(在页面中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点困惑.

我正在使用bit.ly PHP API来缩短一些URL.这在本地主机上可以正常工作-但是,当我在服务器(Apache中运行php的服务器)上尝试使用它时,file_get_contents()返回空字符串.

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.

我检查了我的Apache日志,但Apache看不到任何警告/错误,所以我在CL上尝试了相同的命令(使用PHP CLI):

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?

推荐答案

它可能与PHP配置有关.许多服务器对于CLI和Apache具有不同的php.inis.检查/etc/php5/以查看是否存在多个配置.具体来说,您应该寻找 allow_url_fopen .

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.

实际上,Ubuntu默认附带了该文件,.ini位于/etc/php5/apache2/php.ini

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

这篇关于php:file_get_contents()可与CLI一起使用,但在服务器上调用时不起作用(在页面中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆