簡體   English   中英

樣式化 Google Api GET 請求

[英]Styling Google Api GET request

我試圖弄清楚如何對結果進行樣式設置,例如 formatted_address、formatted_phone_number、location 等。 來自 google api GET 請求。 我知道傳入的數據看起來像這樣。

 "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{ border: 2px solid green; width:500px; height:500px; margin: 0 auto; display:table; }
 <?php //The URL with parameters / query string. $url = 'https://maps.googleapis.com/maps/api/place/details/json?place_id=ID GOES HERE&fields=name,rating,reviews,geometry,opening_hours,formatted_address,formatted_phone_number&key=KEY GOES HERE API'; $url1 = 'https://maps.googleapis.com/maps/api/place/details/json?place_id=ID GOES HERE1&fields=name,rating,reviews,geometry,opening_hours,formatted_address,formatted_phone_number&key=KEY GOES HERE API'; $url2 = 'https://maps.googleapis.com/maps/api/place/details/json?place_id=ID GOES HERE2&fields=name,rating,reviews,geometry,opening_hours,formatted_address,formatted_phone_number&key=KEY GOES HERE API'; $url3 = 'https://maps.googleapis.com/maps/api/place/details/json?place_id=ID GOES HERE3&fields=name,rating,reviews,geometry,opening_hours,formatted_address,formatted_phone_number&key=KEY GOES HERE API'; $url4 = 'https://maps.googleapis.com/maps/api/place/details/json?place_id=ID GOES HERE4&fields=name,rating,reviews,geometry,opening_hours,formatted_address,formatted_phone_number&key=KEY GOES HERE API'; $url5 = 'https://maps.googleapis.com/maps/api/place/details/json?place_id=ID GOES HERE5&fields=name,rating,reviews,geometry,opening_hours,formatted_address,formatted_phone_number&key=KEY GOES HERE API'; //Once again, we use file_get_contents to GET the URL in question. $contents = file_get_contents($url); $contents1 = file_get_contents($url1); $contents2 = file_get_contents($url2); $contents3 = file_get_contents($url3); $contents4 = file_get_contents($url4); $contents5 = file_get_contents($url5); $contents6 = file_get_contents($url6); $contents7 = file_get_contents($url7); //If $contents is not a boolean FALSE value. if($contents !== false){ //Print out the contents. echo '<div class="test">'; echo $contents,$contents2,$contents3,$contents4,$contents5; echo '</div>'; } var_dump($array);//will equal null. ?>

您可以解碼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_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>'
// ...

另請注意,最好使用數組和循環,以防止重復代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM