Php 设计GoogleAPI获取请求的样式
我试图弄清楚如何将结果格式化,比如格式化的地址、格式化的电话号码、位置等等。从GoogleAPI获取请求。我知道传入的数据将是这样的Php 设计GoogleAPI获取请求的样式,php,html,css,api,Php,Html,Css,Api,我试图弄清楚如何将结果格式化,比如格式化的地址、格式化的电话号码、位置等等。从GoogleAPI获取请求。我知道传入的数据将是这样的 "html_attributions" : [], "result" : { "formatted_address" : "addy", "formatted_phone_number" : "phone", "geom
"html_attributions" : [],
"result" : {
"formatted_address" : "addy",
"formatted_phone_number" : "phone",
"geometry" : {
"location" : {
"lat" : 00.00000000000000,
"lng" : -00.0000000
},
"viewport" : {
"northeast" : {
"lat" : 00.0000000000000,
"lng" : -00.0000000000000
},
"southwest" : {
"lat" : 00.0000000000000,
"lng" : -00.0000000000000
}
}
},
我假设它是一个数组。它将吸引8种不同的业务。对我来说,从谷歌收到的每个api请求,有什么简单的方式来设计它们的样式?如果这很容易的话,我对php很陌生。我知道我可以打印出HTML并设置样式,感觉所有数据的格式都是一样的,我可以做预定义的样式,因为每次对每个数组使用相同的方式。或者我可以每周提取一次数据并保存到sql数据库中,以减少带宽和更快的加载时间。但是一步一个脚印,让我们先试试这个。我想学习
.test{
边框:2倍纯绿;
宽度:500px;
高度:500px;
保证金:0自动;
显示:表格;
}
您可以解码json响应数据,并在其中打印数据。
另外,要单独设置每个请求的样式,只需在样式表中添加两个类,并使用这些类打印它们
.test{
border: 2px solid green;
width:500px;
height:500px;
margin: 0 auto;
display:table;
}
.test.test1{
border-color: red;
}
.test.test2{
border-color: blue;
}
...
$contents1=json\u解码($contents1,true);
$contents2=json_decode($contents2,true);
// ...
回声“
如果($contents1!==false){
回声';
回显'.$contents1['result']['formatted_address']。'';
回声';
}
如果($contents2!==false){
回声';
回显'.$contents2['result']['formatted_address']。'';
回声';
}
回声“
// ...
还要注意,最好使用数组和循环,以防止代码重复。您是否进行了var_dump($contents)以查看实际返回的内容(如果有)。当您编写关于对API的GET请求时,我假设您指的是一个curlrequest@qwertyyuiop更新了答案,包括
json\u decode
@qwertyyuiop没问题。欢迎来到堆栈溢出。当你得到你想要的答案时,请将该答案标记为已接受答案。这是一个已解决的
标签,帮助其他人知道此问题已解决。happy codding:)你能给我解释一下吗?$contents1['result']['formatted_address']我知道['result']将是正确的数组,['formatted_address']是你从数组中获取的内容吗?@qwertyyuiop它们不是数组。它们是dictionary(object):这应该可以使用$contents2['result']['reviews']['rating']但会给出字符串(8)“[rating]”“我不需要在这一点上指定键0,类似于['rating[0]]”???
$contents1 = json_decode($contents1,true);
$contents2 = json_decode($contents2,true);
// ...
echo '<div class="container">'
if($contents1 !== false){
echo '<div class="test test1">';
echo '<span class="address">' . $contents1['result']['formatted_address'] . "</span>";
echo '</div>';
}
if($contents2 !== false){
echo '<div class="test test2">';
echo '<span class="address">' . $contents2['result']['formatted_address'] . "</span>";
echo '</div>';
}
echo '</div>'
// ...