如何在json对象中调用特定数据

时间:2018-01-27 01:02:03

标签: php json wordpress

好的我正在调用google places API并获取一个JSON对象。我已经能够使用Ajax使用此对象,但现在我必须将我的代码集成到Wordpress实例中,并且我在functions.php中进行API调用,因为我想使用get_theme_mod();方法

因为我对php很新,所以我不知道如何从这个对象输出特定的数据。

我的对象如下,我正在尝试访问此对象返回的评论(全部5)。我知道我可以很容易地用Ajax做到这一点,但我真的很难用PHP中的语法。

这是我的PHP API调用

function do_api_call(){
$url = $my_url;
$data = file_get_contents($url);

$rev = json_encode($data);
echo $rev;

}



{"html_attributions":[],"result":{"address_components":[{"long_name":"76","short_name":"76","types":["street_number"]},{"long_name":"North Main Street","short_name":"N Main St","types":["route"]},{"long_name":"Concord","short_name":"Concord","types":["locality","political"]},{"long_name":"Merrimack County","short_name":"Merrimack County","types":["administrative_area_level_2","political"]},{"long_name":"New Hampshire","short_name":"NH","types":["administrative_area_level_1","political"]},{"long_name":"United States","short_name":"US","types":["country","political"]},{"long_name":"03301","short_name":"03301","types":["postal_code"]},{"long_name":"4915","short_name":"4915","types":["postal_code_suffix"]}],"adr_address":"<span class=\"street-address\">76 N Main St</span>, <span class=\"locality\">Concord</span>, <span class=\"region\">NH</span> <span class=\"postal-code\">03301-4915</span>, <span class=\"country-name\">USA</span>","formatted_address":"76 N Main St, Concord, NH 03301, USA","formatted_phone_number":"(603) 856-7181","geometry":{"location":{"lat":43.20638719999999,"lng":-71.53594079999999},"viewport":{"northeast":{"lat":43.20770828029149,"lng":-71.53477561970848},"southwest":{"lat":43.2050103197085,"lng":-71.5374735802915}}},"icon":"https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png","id":"9496ddb6eb5f976d65d829a5bc2a11c16235f24c","international_phone_number":"+1 603-856-7181","name":"The Crazy Goat","opening_hours":{"open_now":true,"periods":[{"close":{"day":0,"time":"1800"},"open":{"day":0,"time":"1200"}},{"close":{"day":2,"time":"2200"},"open":{"day":2,"time":"1200"}},{"close":{"day":3,"time":"2200"},"open":{"day":3,"time":"1200"}},{"close":{"day":4,"time":"2200"},"open":{"day":4,"time":"1200"}},{"close":{"day":5,"time":"2200"},"open":{"day":5,"time":"1200"}},{"close":{"day":6,"time":"2200"},"open":{"day":6,"time":"1200"}}],"weekday_text":["Monday: Closed","Tuesday: 12:00 – 10:00 PM","Wednesday: 12:00 – 10:00 PM","Thursday: 12:00 – 10:00 PM","Friday: 12:00 – 10:00 PM","Saturday: 12:00 – 10:00 PM","Sunday: 12:00 – 6:00 PM"]},"photos":[{"height":1536,"html_attributions":["<a href=\"https://maps.google.com/maps/contrib/109874917867182741893/photos\">The Crazy Goat</a>"],"photo_reference":"CmRaAAAAuVKW68MbnuWHZyE8nw6AfMeYfqR8hoZBNmlFFReExDzbgqrkY2NtZro_f8cBWLRzVTajatvGjpv9Mffqv_zeCFuGJIz0jBreCN-0d8YQkc11gzjwZT_-TVS-dwvvx0efEhD_NXSEyy9Q7VZJd2tIFK_iGhT-pd1t-6YpKzhr8nVvaGlfFv36gw","width":2048},{"height":3024,"html_attributions":["<a href=\"https://maps.google.com/maps/contrib/115623591626747192241/photos\">a beauregard</a>"],"photo_reference":"CmRaAAAA8jPB2uPOxWd_-1inL9GMO7tWdIfhvaVCYFODb2yessHQ_Jy7Wyo99z9Qrlohho0tdFlz4GyuGgtO1CEirdeLYJJb25QbX4ZdBClQwDjYBDUC0spd_tPFLAnsFCLk664ZEhBGqm26u-aeqnDBQzWKPTc2GhR2OUmpnyErQr3rsbmOC92nva595A","width":4032},{"height":2988,"html_attributions":["<a href=\"https://maps.google.com/maps/contrib/111065387557395503043/photos\">Vicky and Snickers Anderson</a>"],"photo_reference":"CmRZAAAAk-Z3Ml_aMD8_xwpZBT85JDfd9dtrq_LponeZICHGhbRp1KozF4H8Ig36ikIYcbJyH0DWf-4_ylRWviWYDBkuJWcKyvHRSzjquxuE_MNN5FjjkWX3aPBcNWxTnh9RQ8OcEhAE9Y91__B86071fFBuZFDAGhSF5XxgaAxsUl8Im4_lgVCMIgGPNA","width":5312},{"height":4032,"html_attributions":["<a href=\"https://maps.google.com/maps/contrib/115623591626747192241/photos\">a beauregard</a>"],"photo_reference":"CmRaAAAAgaVjlQzuv7F-88pv8GPd-IkGzo9oH1gQoKc_ZmVmQjVqWeeljkVLSsti3oLzkau3IXW3JOJ7KyOvlsOUTGNOJnBA31OIttfCbmCFPSCiyywmu7yUHnf_w0CKLw5hT_RWEhCpSZtnAKj3umsNcJUtI7K3GhR4g14ff5ekvKuB_rW0YlL_FWotxg","width":3024},{"height":3024,"html_attributions":["<a href=\"https://maps.google.com/maps/contrib/115623591626747192241/photos\">a beauregard</a>"],"photo_reference":"CmRaAAAA3so1UtaPCAqJlbpxhyfbBefycY4FspxrNPDBH-xeBoTMTgkTCb3uT9p8l3Umgu2VpCatqTu3fDFgbj5VmjNGvk7ngtYYhD2UCMXoCxuxHjL9kzvoaDdL8SoyJUFWBnlZEhCeTxLqrBsM8R6IPd8iK_h2GhTmgvfEor4xDZNha0teaiw3KyHhrQ","width":4032},{"height":4032,"html_attributions":["<a href=\"https://maps.google.com/maps/contrib/111580933990207571063/photos\">Michael Brady</a>"],"photo_reference":"CmRaAAAAlxoITLsevfPLrsV5rCHt1IuJBs-sKE6tTuQmmKg6xbAi2puyrhlG3aEY9GoFUCRpApEL30TssUQPN31U6kkCdIDofmJktXYvpvJ9nb2EItJDyVE32s7XE15XJEx3mvPKEhA19O6fhPnSKbSD2bE-lTqPGhRjw-0d1TspjpQC3yhez71pWyJKOA","width":3024},{"height":2592,"html_attributions":["<a href=\"https://maps.google.com/maps/contrib/110913511470452374693/photos\">Maxwell Murdoch Snelling</a>"],"photo_reference":"CmRaAAAApttZtrF9LRbcVHeCokgPipfdh-Gs-v6G9ze8vAXh2YPP8PEx3cWy7AEgY72_79VFPAMWTkKgbrOceNiqn7t6R0USgUvD9hnPq_WLc9jPt-Fbwt0HwoO2QZtb0l7Xry_IEhD9SRgHx9fiVLGWyxfIs67LGhSprzTG6Srq3f4HkpBZhytA__2wvQ","width":1944},{"height":3024,"html_attributions":["<a href=\"https://maps.google.com/maps/contrib/115623591626747192241/photos\">a beauregard</a>"],"photo_reference":"CmRaAAAA2lGLeytP1Eo0nIe_a-ml4eW9xdMXbiTo8BQij_tL2IBtd9Qd1pJDEhPEF-4ypXhraLSgTLa9Bv8D6pOelpvRoEw3GoO5_G1ZPyV5cDqqRXYEaZL7AGN77otQ0Qo50rb9EhBK4oPCeFf4BKD5_lyjfqKmGhR59yKmhuLhv-qTKzyD0jlau1YwHQ","width":4032},{"height":5312,"html_attributions":["<a href=\"https://maps.google.com/maps/contrib/110848241892026538641/photos\">Dennis Shanoff</a>"],"photo_reference":"CmRaAAAAdlDGdD00jBf5hOZ49bpJUfjPzH0m0G5sIXOkP5KEqbksfqL33k-9uxUyW3LqhJjat4Jbggq6C_8bmfsclSFjdhJz2NayfOsE-nyyMNEN-GwPtZfhpc50HT9Nkn5jKNWxEhCCkJ6WpbssuAGn-JdibF_LGhSPJDtJGvkFcwb6HRhoVxWBrR4EbQ","width":2988},{"height":3024,"html_attributions":["<a href=\"https://maps.google.com/maps/contrib/115623591626747192241/photos\">a beauregard</a>"],"photo_reference":"CmRaAAAAL5u0ksBwwzBYwycDHNqTEtKqdaGpPRB9REBvNVl_6E4HgiuEOkYpZphocizNd-EozrlE05Zwz1wC2ZcibsqF0CI3-wP8yJyFC76Lu-yZkHrNyAUxs85GGg8xOLTUAzlXEhBQne5bG7eQQn8pW8DszBqVGhQqkbf-vt6WC8QRdGzuGeUpoYQaww","width":4032}],"place_id":"ChIJbQa_U41q4okRJsbAM3zX_WM","rating":3.4,"reference":"CmRRAAAAPU4b-A3ebudL3C27zOGt3pH0uqRXiRmY_feRH3O4SXJpIB7ghnQRsOwDRe0nUssISkr3V_pdwv6BXxHeMvneYGUWLFOTRe_aUlnDQKGpkrXj3bOoFBx7QGdo5ZWgJbKpEhDIJX5_r27SlpSsb9hLIpHZGhSC_JiK3IVPnom3nuQLNOOXZUFsaw","reviews":[{"author_name":"Papa Joe Gaudet","author_url":"https://www.google.com/maps/contrib/111381118825182249763/reviews","language":"en","profile_photo_url":"https://lh4.googleusercontent.com/-lJEMylIg98M/AAAAAAAAAAI/AAAAAAAATvY/njN-66RVAp0/s128-c0x00000000-cc-rp-mo-ba5/photo.jpg","rating":5,"relative_time_description":"2 weeks ago","text":"I dropped in on a cold quiet Tuesday evening. The food was outstanding as was the service. I wish I had more time to enjoy the atmosphere. Next time (and there will be a next time), I'll bring a date. She'll very likely appreciate my good taste. BTW, they are hiring right now and the staff looks happy.","time":1515541362},{"author_name":"Christine D'","author_url":"https://www.google.com/maps/contrib/110635532521172813159/reviews","language":"en","profile_photo_url":"https://lh6.googleusercontent.com/-neX3OzkbUe4/AAAAAAAAAAI/AAAAAAAAAAA/AA6ZPT6K8ZOkQiewnkB6bxg3VaKuVFIa5Q/s128-c0x00000000-cc-rp-mo-ba4/photo.jpg","rating":5,"relative_time_description":"a week ago","text":"Late lunch on a Friday afternoon. We had sandwiches (a burger and chicken sand.) which were both quite good. Flat bread with goat cheese bruschetta app was delicious! Waiter/bartender was knowledgeable and SO personable. Even if we had struck out with our food selections he would be reason enough to go back. He engaged with every patron in the place. The only thing I'm sorry about is that I didn't get his name. The Crazy Goat is lucky to have him.","time":1515820810},{"author_name":"Lee Goodwin","author_url":"https://www.google.com/maps/contrib/117058651202426601746/reviews","language":"en","profile_photo_url":"https://lh4.googleusercontent.com/-XcqH9W_RTeY/AAAAAAAAAAI/AAAAAAAAAEI/Y75Cnl2eE_k/s128-c0x00000000-cc-rp-mo/photo.jpg","rating":5,"relative_time_description":"a month ago","text":"The food is delicious. I had a special of stuffed roasted acorn squash, unreal. The atmosphere and server John were excellent. Can't wait to come here again!","time":1512852799},{"author_name":"happy2say1hello","author_url":"https://www.google.com/maps/contrib/109024209580809571111/reviews","language":"en","profile_photo_url":"https://lh3.googleusercontent.com/-Oct8pO1R3w4/AAAAAAAAAAI/AAAAAAAAAAA/AA6ZPT4gyixHn6uuGcL_-2l1PmC9ztLTUg/s128-c0x00000000-cc-rp-mo/photo.jpg","rating":1,"relative_time_description":"a month ago","text":"I worked here and the owners treated the staff like garbage. Worst job experience I've ever had. Not to mention the food wasn't that great but so expensive. Please save your money for a restaurant that treats its staff better.","time":1512504706},{"author_name":"Tracy Long","author_url":"https://www.google.com/maps/contrib/102667733449818335247/reviews","language":"en","profile_photo_url":"https://lh4.googleusercontent.com/-uc5qHDhCNjs/AAAAAAAAAAI/AAAAAAAAAAA/AA6ZPT6KjwgNKU-cDRvtgpFDyG809i0BlA/s128-c0x00000000-cc-rp-mo/photo.jpg","rating":3,"relative_time_description":"in the last week","text":"Server was very personable and friendly. Food was ok. Very limited menu.  Liked the ambiance.","time":1516936344}],"scope":"GOOGLE","types":["restaurant","food","point_of_interest","establishment"],"url":"https://maps.google.com/?cid=7205151907306849830","utc_offset":-300,"vicinity":"76 North Main Street, Concord","website":"http://www.crazygoatconcord.com/"},"status":"OK"}

1 个答案:

答案 0 :(得分:1)

试试这个:

$rev = json_decode($data,true);
$reviews = $rev["result"]["reviews"];
foreach($reviews as $review){
    $author_name = $review["author_name"];
    echo $author_name."<br>";
}