php - 如何修复 php 警告 : file_get_contents? 无法打开流:HTTP 请求失败

标签 php file-get-contents

如何修复 php 警告:file_get_contents?

Warning: file_get_contents(http://192.168.1.254:9999/api/p/internal/ucenter/login?loginName=test102&password=111111) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /Applications/XAMPP/xamppfiles/htdocs/PHP_test/index.php on line 49

这是与 $files 相关的代码:

<?php
$loginNames="test102";
$passwords="111111";
$apiUrl = "http://192.168.1.254:9999/api/p/internal/ucenter/login?loginName=".$loginNames."&password=".$passwords;
$callback = file_get_contents($apiUrl);

print_r($callback);
//echo $callback;
?>

最佳答案

如果这显示在您的页面中,则说明您的 display_errors 有问题设置。

理想情况下,对于生产机器,display_errors 应该是 Off 并且您应该使用 set_error_handler() 自行处理错误.

另请参阅:Error logging, in a smooth way

除非您确保该页面存在,否则无法停止此特定警告。但是,单独使用时,您可以使用 muffle operator防止出现警告:

if (false !== ($contents = @file_get_contents('...'))) {
    // all good
} else {
    // error happened
}

请注意,这仍会调用您的自定义错误处理程序

关于php - 如何修复 php 警告 : file_get_contents? 无法打开流:HTTP 请求失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12791974/

相关文章:

php - PHP文件获取内容给出404错误但存在

php - SilverStripe:如何向另一个网站发出 HTTP 请求?

php - 以 url 作为变量的 file_get_contents

php - SQL 备份无法与 PHP 脚本一起使用

php - 使用 jquery mysql 点击填充多个字段

php - file_get_contents 与 Lumen

php - 当我想使用 file_get_contents() 时超时

php - 无法连接到数据库 : Access denied for user '' @'localhost' to database 'socialdb'

php - SQLiteResult::numColumns 在数据库为空时返回 1 (PHP)

php - 使用 PHP 生成包含大量行的 JSON