Curl 谷歌翻译api的麻烦

Curl 谷歌翻译api的麻烦,curl,google-translate,Curl,Google Translate,我正在尝试在我的webapp中使用googletranslateapi。我使用一些ajax将翻译加载到div中,但是当我在eclipse中的笔记本电脑上执行此操作时,大约有50%的时间会出现错误,表示json响应中没有索引“数据”。我想这是因为api还没有响应,所以我希望在这里放一个循环,让代码等待,但我不确定在哪里。下面是生成xml响应的php页面 <?php header('Content-type: text/xml'); $cc_nlc = 'good'; if(isset($_

我正在尝试在我的webapp中使用googletranslateapi。我使用一些ajax将翻译加载到div中,但是当我在eclipse中的笔记本电脑上执行此操作时,大约有50%的时间会出现错误,表示json响应中没有索引“数据”。我想这是因为api还没有响应,所以我希望在这里放一个循环,让代码等待,但我不确定在哪里。下面是生成xml响应的php页面

<?php 
header('Content-type: text/xml');
$cc_nlc = 'good';
if(isset($_POST['cc_nlc'])){
    $cc_nlc = $_POST['cc_nlc'];
    if(empty($cc_nlc)){
        $cc_nlc = 'not very good';
    }
}
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
echo '<response>';
echo '<translation>';

$apikey = 'my key';

$url = 'https://www.googleapis.com/language/translate/v2?key=' . $apikey . '&q=' . rawurlencode($cc_nlc) . '&source=en&target=ar';
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($handle);
$responseDecoded = json_decode($response, true);
curl_close($handle);
//print_r($responseDecoded);
$translation = $responseDecoded['data']['translations'][0]['translatedText'];

echo $translation;

echo '</translation>';
echo '</response>';
?>