开发者

REST method in calling google translate API

I want to access the Google Translate APIv2. Here is the url:

https://www.googleapis.com/language/translate/v2?key=INSERT-YOUR-KEY&q=hello%20world&source=en&target=de

I already have the 开发者_StackOverflowkey and this url is working fine if I am using it in the browser address bar.

My problem is that I do not know how to call this url using REST. I want to get the result stored in a variable.


Try

<?php

//make http request
$response = file_get_contents('https://www.googleapis.com/language/translate/v2?key=INSERT-YOUR-KEY&q=hello%20world&source=en&target=de');

//decode json to array
$json = json_decode($response);

//show the json array in a readable format
echo '<pre>';

//show array
print_r($json);

?>

You can then access specific nodes with echo $json['key_name']

Useful URLs:

http://php.net/manual/en/function.file-get-contents.php

http://php.net/manual/en/function.json-decode.php


The simplest way would be file_get_contents.


You could use jQuery and use the $.getJSON, I am no jQuery expert but have used this method several times, it is simple and powerful. Here is the doc for it http://api.jquery.com/jQuery.getJSON/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜