【问题标题】:php file_get_contents() works on test server but not on production serverphp file_get_contents() 适用于测试服务器但不适用于生产服务器
【发布时间】:2018-04-25 08:13:21
【问题描述】:

我的代码在测试服务器中,它使用 file_get_contents() 从 url 获取状态。测试服务器链接为: http://test1one.epizy.com/portal/test_api.php 它可以正常工作,并从 url 给我适当的“活动”或“非活动”响应。

但它在生产服务器链接上不起作用并给出一个空白页面: http://thecreamplatform.com/portal/test_api.php 链接页面包含以下代码行:

$url = "http://209.208.78.58:8085/cream/verify?msisdn=23480xxxxxxxx";

try {
    $result = file_get_contents($url);
    echo "<pre>"; print_r($result);
} catch (Exception $e) {
    echo "<h1>Error!!!</h1>", $e->getMessage(), "\n";
}

上面代码中的xxxxxxxx指的是手机号码。任何人都可以帮我解决导致问题的原因吗?提前致谢。

【问题讨论】:

  • 请定义“不起作用”。它会发出错误吗?它是否在日志中记录错误?它会打开错误的文件吗?地狱深处的恶魔会不会被释放到这个世界上来吞噬可怜罪人的肉体?如果没有更多信息,我猜这可能是权限问题,但这只能是猜测。
  • 另外,try/catch 在处理 file_get_contents 时无效,因为它不会抛出异常,它会发出警告并返回非值(我认为是 null)。
  • 检查 URL 可能是返回 404
  • file_get_contents() 确实不会抛出异常,但您可以捕获错误(可抛出接口)catch (\Throwable $e)。 PHP 7+ 或使用 ini_set('display_errors', 1); ini_set('error_reporting', E_ALL); 启用错误,位于文件顶部。

标签: php file-get-contents


【解决方案1】:

检查http://php.net/file_get_contents。如果返回值为NULL,则该功能被禁用。

启用错误报告和错误显示,以了解有关问题的更多信息。

error_reporting(~0);
ini_set('display_errors', 1);

【讨论】:

猜你喜欢
  • 2023-03-04
  • 2015-07-18
  • 1970-01-01
  • 1970-01-01
  • 2015-02-10
  • 1970-01-01
  • 1970-01-01
  • 2012-05-11
  • 1970-01-01
相关资源
最近更新 更多