php 命令行和服务器之间的区别


Difference between php commandline and server

如果我运行

php -r 'print_r(file_get_contents("http://mirror.facebook.net/centos/timestamp.txt"));'

我收到"星期二 Oct 28 13:24:01 UTC 2014"(如预期的那样(。

但是如果我有一个 php 文件:

<?php
print_r(file_get_contents("http://mirror.facebook.net/centos/timestamp.txt"));

它给了我:

**Warning:** file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known ...
**Warning:** file_get_contents(http://mirror.facebook.net/centos/timestamp.txt): failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known ...

"php -r"和php从文件中运行代码有什么区别


更新

这不是问题的答案,但它确实解决了我的问题,所以为了未来的谷歌用户:我不知道这两个解决方案中的哪一个是关键,但当我重新启动 apache 时它正在工作。

  1. 在 resolv.conf 中设置nameserver 8.8.8.8(其中有我的路由器地址(

  2. 有一个必须打开的 SELinux 布尔httpd_can_network_connect

塞布尔 -P httpd_can_network_connect 1

请参阅此解决方案和此解决方案。

您的 PHP CLI 正在使用不同的php.ini(或者可能根本没有php.ini(。 您运行的 PHP 二进制文件也可能与服务器使用的 PHP 二进制文件完全不同。

可以在命令行上运行php -i,并在脚本中phpinfo()以确定正在使用的php.ini