PHP If file_get_contents does not work, do it instead

I have a function to translate the current text string using the Free Bing API. I just want to make sure that something will not succeed or that something is happening with the application identifier or if I click on the prompts, I don’t want to show a big error.

The code I have now is:

$translate_feed = file_get_contents('http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=' . BING_APPID . '&text=' . urlencode($text) . '&from=en&to=' . $to_lan . ''); $translate = simplexml_load_string($translate_feed); return $translate[0]; 

I want something to happen if something doesn’t work, so if I add another character to the URL to make it invalid, I want it to just return $text , at least that- that shows.

Thanks!

+4
source share
7 answers

Have you really tried this to not understand what is happening?

If this is an exception, just catch it and handle it ...

 try{ //enter code to catch }catch(Exception $ex){ //Process the exception } 

If there is an error output by the function, simply @ to hide the error and handle the incorrect output of $ translate_feed or $ translate manually.

You can try to compromise it by simply passing an invalid URI to file_get_contents, and then force the non-XML or invalid XML into simplexml_load_string to see what happens.

+15
source
 $translate_feed = file_get_contents('http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=' . BING_APPID . '&text=' . urlencode($text) . '&from=en&to=' . $to_lan . ''); if ( $translate_feed === false ) { echo "failed"; } 
+5
source
 /* It a modified file_get_contents() get_contents(filename, use_include_path, context, offset, maxlen) */ function get_contents($url, $u = false, $c = null, $o = null) { $headers = get_headers($url); $status = substr($headers[0], 9, 3); if ($status == '200') { return file_get_contents($url, $u, $c, $o); } return false; } echo get_contents('https://batas.kz/'); 
+2
source

if( !$translate_feed) return "Failed";

0
source
 $translate_feed = file_get_contents('http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=' . BING_APPID . '&text=' . urlencode($text) . '&from=en&to=' . $to_lan . ''); $result=$text; if ($translate_feed) { $translate = simplexml_load_string($translate_feed); if (is_array($translate)) $result=$translate[0]; } return $result; 
0
source

You can do it as follows:

 if(@file_get_contents("yourFilePath.txt")){ echo "success"; } 
0
source

$ http_response_header is created in the local area, which we can use to check the headers returned from the server. Here is my implementation:

 public function getData($url) { try { $response = @file_get_contents($url); if (isset($http_response_header)) { if (!in_array('HTTP/1.1 200 OK', $http_response_header) && !in_array('HTTP/1.0 200 OK', $http_response_header)) { throw new \Exception('Server did not return success header!'); } } return $response; } catch (\Exception $ex) { throw new TransportException($ex->getMessage(), $ex->getCode(), $ex); } } 
0
source

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


All Articles