时间:2021-07-01 10:21:17 帮助过:17人阅读
调用百度API的url为'http://openapi.baidu.com/public/2.0/translate/dict/simple?client_id=你的KEY&q=要查的汉语&from=zh&to=en';
申请步骤详见点击打开链接
以上是前期准备工作
===================================================================================================
通过调用百度翻译的API穿回的json为:{"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"}'
这里眼力不好的同学可以格式化一下字符串
{ "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"}
关键语句:json_decode($jsonResult)->data->symbols[0]->parts[0]
也怪我太笨了,就这么一条语句折腾了我一晚上……基础太重要了!!!
通过这一步获取到了
{"part":"", "means":[ "hello", "hi", "How do you do!" ] }
$jsonObj->means[0] $jsonObj->means[1] $jsonObj->means[2]分别获取“你好”的3个释义。
实际操作中可以通过循环语句获取全部的释义。
下面是全部代码
data->symbols[0]->parts[0]; echo $jsonObj->means[0].'输入 url?s=你好
'; echo $jsonObj->means[1].'
'; echo $jsonObj->means[2].'
';?>