Php json code processing example: PHP implements Baidu translation API call processing

Source: Internet
Author: User
Tags php json
Example of php processing json code: PHP implements Baidu translation API call and processing. Today, a small program is written to the background, and Baidu translation API is called to implement translation functions.

The url for calling Baidu API is 'http: // openapi.baidu.com/public/2.0/translate/dict/simple? Client_id = your KEY & q = Chinese to be queried & from = zh & to = EN ';

For details about the application process, click the open link.

The above are preparations

========================================================== ========================================================== ==============================

The json returned by calling Baidu translation API is: {"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 "}'

If you have poor eyesight, you can format the 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]

It's also strange that I am too stupid, so I spent one night with such a statement ...... The basics are too important !!!

Obtained through this step

{"part":"","means":["hello","hi","How do you do!"]}

Next pass
$jsonObj->means[0]$jsonObj->means[1]$jsonObj->means[2]
Obtain the three definitions of "Hello.

In actual operations, you can obtain all definitions through cyclic statements.

Below is all the code

 Data-> symbols [0]-> parts [0]; echo $ jsonObj-> means [0].'
'; Echo $ jsonObj-> means [1].'
'; Echo $ jsonObj-> means [2].'
';?>
Enter url? S = Hello
Running result

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.