How to fix php Warning: file_get_contents? Could not open stream: HTTP request failed

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 the code associated with $ 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; ?> 
+4
source share
3 answers

If this is shown on your page, something is wrong with the display_errors setting.

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

See also: Error Recording, Seamlessly

This particular warning cannot be stopped unless you make sure that the page exists. However, using in isolation, you can use the muffle operator to prevent a warning from appearing:

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

Please note that this will still call your own error handler.

+10
source

I have the best option for you.

Avoid file_get_contents , cURL user.

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. Do you feel an encoding problem, add this: curl_setopt($curl, CURLOPT_ENCODING ,"");

Thanks.

0
source

I suggest checking if the url exists before calling file_get_contents. You can ref page How to check if a URL exists through PHP?

0
source

Source: https://habr.com/ru/post/1438532/


All Articles