在php变量中保存Google API响应
我想调用一个php函数,它将处理一个GoogleAPI调用并保存特定的变量,这样我就可以使用它了。 我知道下面的代码不起作用,但我希望它能让你知道我想做什么。 提前感谢您的建议在php变量中保存Google API响应,php,Php,我想调用一个php函数,它将处理一个GoogleAPI调用并保存特定的变量,这样我就可以使用它了。 我知道下面的代码不起作用,但我希望它能让你知道我想做什么。 提前感谢您的建议 function callGoogleAPI () { // calling the fGoggle spi somehow https://maps.googleapis.com/maps/api/timezone/json?location=45.908133,-77.047119×tamp=147
function callGoogleAPI () {
// calling the fGoggle spi somehow
https://maps.googleapis.com/maps/api/timezone/json?location=45.908133,-77.047119×tamp=1472106746
$dstOffset = dstOffset;
}
当函数执行时,我应该在dstOffset变量中保存值“3600”
多谢各位
维尼这是一个非常基本的问题。您可以通过或调用api,然后使用函数解码json
function callGoogleAPI () {
// calling the fGoggle spi somehow
$content = file_get_contents('https://maps.googleapis.com/maps/api/timezone/json?location=45.908133,-77.047119×tamp=1472106746');
$content = json_decode($content, true);
$dstOffset = $content['dstOffset'];
}
在PHP中使用RESTAPI需要三个步骤 1) 您可以校准API并将返回的数据存储在变量中
$maps_content = file_get_contents('https://maps.googleapis.com/maps/api/timezone/json?location=45.908133,-77.047119×tamp=1472106746');
2) 在关联数组中转换JSON对象
$maps_array = json_decode($content, true);
3) 开始处理数组中的数据
$dstOffset = $content['dstOffset'];
要查看变量的外观,请不要忘记使用var_dump
var_dump($maps_array);