PHP If file_get_contents fails, do this instead

后端 未结 7 1790
谎友^
谎友^ 2021-01-02 12:23

I have a function to translate the current text string using the Free Bing translator API. I just want to make sure if anything fails or something happens with the Applicat

7条回答
  •  醉梦人生
    2021-01-02 13:13

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

提交回复
热议问题