如何修复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;
?>发布于 2012-10-09 10:22:59
如果这显示在您的页面中,则说明您的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
}请注意,这仍将调用自定义错误处理程序
发布于 2016-03-11 19:52:15
我有更好的选择给你。
避免使用用户cURL的file_get_contents。
让我给你看一个例子:
$url = 'http://www.yoururl.com';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
curl_close($curl);注意:你觉得编码有问题吗?添加这个:curl_setopt($curl, CURLOPT_ENCODING ,"");
谢谢。
发布于 2018-04-13 00:33:35
我建议在调用file_get_contents之前检查url是否存在。你可以参考How can I check if a URL exists via PHP?页面
https://stackoverflow.com/questions/12791974
复制相似问题