如何使用php解码显示来自json url的数据?

我无法使用 php json 解码显示来自这个 url 的数据:https : //api.mymemory.translated.net/get? q = Hello%20World! & langpair = en|it


这是数据提供者:https : //mymemory.translated.net/doc/spec.php


谢谢。


我想要的是设置一个表单来提交单词并从他们的 API 中获取翻译。


这是我的代码示例:


<?php


$json = file_get_contents('https://api.mymemory.translated.net/get?q=Hello%20World!&langpair=en|it');


// parse the JSON

$data = json_decode($json);


// show the translation

echo $data;


?>


手掌心
浏览 135回答 2
2回答

慕尼黑的夜晚无繁华

我的猜测是,您可能希望编写一些带有if语句的for 循环来根据需要显示数据:测试$json = file_get_contents('https://api.mymemory.translated.net/get?q=Hello%20World!&langpair=en|it');$data = json_decode($json, true);if (isset($data["responseData"])) {&nbsp; &nbsp; foreach ($data["responseData"] as $key => $value) {&nbsp; &nbsp; &nbsp; &nbsp; // This if is to only display the translatedText value //&nbsp; &nbsp; &nbsp; &nbsp; if ($key == 'translatedText' && !is_null($value)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $html = $value;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }} else {&nbsp; &nbsp; echo "Something is not right!";}echo $html;输出Ciao Mondo!<?php$html = '<!DOCTYPE html><html><head><title>read JSON from URL</title></head><body>';$json = file_get_contents('https://api.mymemory.translated.net/get?q=Hello%20World!&langpair=en|it');$data = json_decode($json, true);foreach ($data["responseData"] as $key => $value) {&nbsp; &nbsp; // This if is to only display the translatedText value //&nbsp; &nbsp; if ($key == 'translatedText' && !is_null($value)) {&nbsp; &nbsp; &nbsp; &nbsp; $html .= '<p>' . $value . '</p>';&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; }}$html .= '</body></html>';echo $html;?>输出<!DOCTYPE html><html><head><title>read JSON from URL</title></head><body><p>Ciao Mondo!</p></body>

陪伴而非守候

经过多次研究,我以这种方式工作:$json = file_get_contents('https://api.mymemory.translated.net/get?q=Map&langpair=en|it');&nbsp;&nbsp;$obj = json_decode($json);&nbsp; &nbsp;echo $obj->responseData->translatedText;谢谢你们。
打开App,查看更多内容
随时随地看视频慕课网APP