Today to a small program to write backstage, by invoking the Baidu translation API to achieve the translation function.
Call the URL of Baidu API ' http://openapi.baidu.com/public/2.0/translate/dict/simple?client_id= your key&q= to check the Chinese &from=zh &to=en ';
Application procedure See click Open link
Above is the preparatory work
===================================================================================================
The JSON translated by calling Baidu Translator is: {"errno": 0, "data": {"Word_name": "\u4f60\u597d", "symbols": [{"Ph_zh": "N\u01d0 H\u01ceo", " Parts ": [{" "": "", "means": ["Hello", "hi", "How does You Do!"]}]}]}, "to": "en", "from": "ZH"} '
A classmate with bad eyesight can format a string
{
"errno":0,
"data":{
"word_name":"\u4f60\u597d",
"symbols":[
{"ph_zh":"n\u01d0 h\u01ceo",
"parts":[
{"part":"",
"means":[
"hello",
"hi",
"How do you do!"
]
}
]
}]
},
"to":"en",
"from":"zh"
}
Key statement:Json_decode ($jsonResult)->data->symbols[0]->parts[0]
Also blame me too stupid, just such a statement toss me all night ... The foundation is too important!!!
Through this step, we get the
{"part":"",
"means":[
"hello",
"hi",
"How do you do!"
]
}
Next through
$jsonObj->means[0]
$jsonObj->means[1]
$jsonObj->means[2]
get the 3 definitions of "Hello" separately.
In the actual operation, the whole interpretation can be obtained through the loop statement.
Here's all the code
<?php
$word=$_GET['s'];
$url='http://openapi.baidu.com/public/2.0/translate/dict/simple?client_id=你的KEY&q='.$word.'&from=zh&to=en';
$jsonResult=file_get_contents($url);
$jsonObj=json_decode($jsonResult)->data->symbols[0]->parts[0];
echo $jsonObj->means[0].'<br />';
echo $jsonObj->means[1].'<br />';
echo $jsonObj->means[2].'<br />';
?>
Input url?s= Hello
Run Results
PHP Processing JSON code example: PHP implementation of Baidu translation API call processing