PHP Processing JSON code example: PHP implementation of Baidu translation API call processing

Source: Internet
Author: User



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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.