4

How to fix php Warning: 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

Here is code related to $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;
?>
小弟调调
  • 1,315
  • 1
  • 17
  • 33

3 Answers3

12

If this shows in your page, there's something wrong with your display_errors setting.

Ideally, display_errors should be Off for production machines and you should handle errors by yourself using set_error_handler().

See also: Error logging, in a smooth way

This particular warning can't be stopped unless you make sure the page exists. However, used in isolation, you can use the muffle operator to prevent the warning from appearing:

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

Note that this will still call your custom error handler

Community
  • 1
  • 1
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
1

I have better option for you.

Avoid file_get_contents , user cURL .

Let me show you one example:

$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);

Note: Is you feel encoding problem add this: curl_setopt($curl, CURLOPT_ENCODING ,"");

Thanks.

Ronak Patel
  • 3,324
  • 4
  • 21
  • 31
0

I suggest try to check if url exist before you call file_get_contents. You can ref page How can I check if a URL exists via PHP?

Kodomo
  • 53
  • 4