简体   繁体   English

使用Yelp API从JSON响应获取数据

[英]Getting data from json response using Yelp API

I have a client who's using the Yelp API to get the 10 closest results to a point using a latitude and longitude. 我有一个使用Yelp API使用纬度和经度获得最接近点的10个结果的客户端。 The api query combines a "list" of categories together and then passes it to the query in Yelp. api查询将类别的“列表”组合在一起,然后将其传递给Yelp中的查询。

Something like this: 像这样:

 $categories = [ 'pharmacy', 'hospitals', 'firedepartments', 'policedepartments', 'parks', 'restaurants', 'grocery' ];

 $list = join($categories, ",");

When that call is made, Yelp doesn't seem to return all the category alias' correctly. 进行该调用时,Yelp似乎无法正确返回所有类别别名。 But, when each category is called individually, it does return the category alias' right. 但是,当分别调用每个类别时,它确实返回类别别名的权利。

So when I use a foreach() statement to loop the categories, like this: 因此,当我使用foreach()语句循环分类时,如下所示:

    foreach($categories as $k=>$v){

      $resulted = yelp_search($v, $range, $lat, $lon, $num);
      $resulted = json_decode($resulted, true);
      $result[] = array_merge($result, $resulted);
    }

It will add it to the $result array. 它将添加到$ result数组中。 But the problem is, that it will only add the last category to the list, for example in this case "grocery". 但是问题在于,它只会将最后一个类别添加到列表中,例如在这种情况下为“食品杂货”。 So when the function in javascript is selected for "closest" it will only show the grocery icons on the map and in the list, not everything it returns. 因此,当将javascript中的函数选择为“最近”时,它将仅在地图上和列表中显示杂货店图标,而不显示其返回的所有内容。

That's issue #1, issue #2 is this. 这是问题1,问题2是这个。 In the list displayed, because 'category' is being set as 'closest' to a function in javascript, I have to go through and look at the categories in the json data returned, and check if it matches something in the list. 在显示的列表中,由于将“类别”设置为与javascript中的函数“最接近”,因此我必须仔细检查返回的json数据中的类别,并检查其是否与列表中的某些内容匹配。

like this 像这样

                    for (var i = 0; i < count; i++) {
                  var b = results[category]['businesses'][i];

                  if(category == "closest"){

                      ccount = b.categories.length;

                      for(var m = 0; m < ccount; m++){
                        console.log(b.categories[m]['alias']);

                        var alias = cats.includes(b.categories[m]['alias']);

                        if(alias){

                            var c = b.categories[m]['alias'];
                            break;

                        }
                        else{
                            var c = results[category]['category_name'];
                        }
                      }



                  update = update + '<tr><td role="button" class="td_menu" data-category="' + c + '" data-index="' + i + '"><img style="height: 20px; padding-right: 5px;" src="<?php echo get_template_directory_uri(); ?>/images/' + c + '.png"><span style="vertical-align: top;">' + b.name + 
                '</span></td><td></td><td>' + (b.distance * 0.00062137).toFixed(2) + '</td><td>mi</td></tr>\n';               

                  }
                  else{

                  update = update + '<tr><td role="button" class="td_menu" data-category="' + category + '" data-index="' + i + '"><img style="height: 20px; padding-right: 5px;" src="<?php echo get_template_directory_uri(); ?>/images/' + category + '.png"><span style="vertical-align: top;">' + b.name + 
                '</span></td><td></td><td>' + (b.distance * 0.00062137).toFixed(2) + '</td><td>mi</td></tr>\n';                   


                  }



                }

All of this part seems to work OK, because it does return the proper category alias (aka grocery) and the icons in the list do show. 所有这部分似乎都可以正常工作,因为它确实返回了正确的类别别名(又名食品杂货店),并且列表中的图标也确实显示了。 I suspect that once I can figure out how to get issue #1 resolved, issue #2 will fix itself. 我怀疑一旦我弄清楚如何解决问题#1,问题#2就会解决。

I need some guidance on this please. 我需要一些指导。 I've spent 2 days trying to get this right and to show the proper icons in the list, as well as getting the closest data so that it does contain all the alias' in the data. 我花了2天的时间来解决这个问题,并在列表中显示适当的图标,并获取最接近的数据,以便它确实包含数据中的所有别名。

The Yelp answer contains also metadata, you cannot just concatenate them : you want to merge the businesses only : Yelp的答案还包含元数据,您不能仅将它们连接起来:您只想合并业务:

$result = array();
foreach($categories as $k=>$v){
  $resulted = yelp_search($v, $range, $lat, $lon, $num);
  $resulted = json_decode($resulted, true);

  if(array_key_exists('businesses', $result))
    $result['businesses'] = array_merge($result['businesses'], $resulted['businesses']);
  else
    $result = $resulted ;
}

Note that the metadata are wrong (the total for example), maybe you need to update that too if you need it. 请注意,元数据是错误的(例如, total ),也许您也需要对其进行更新。 Yelp returns always the most specific keywords, so even with 'restaurants', you will have 'libanese' or 'pizza' and not 'restaurants'. Yelp总是返回最具体的关键字,因此即使使用“餐厅”,您也将拥有“ libanese”或“ pizza”而不是“ restaurants”。 If you need this keyword for displaying a marker, you can add it to the list if it is not already there. 如果需要此关键字来显示标记,可以将其添加到列表中(如果尚未存在)。

Yelp API returns the distance from the location you are looking at : once you have your whole collection, you can sort your whole array per decreasing distance : Yelp API会返回您正在查看的位置的距离:收集完整个集合后,您可以按递减的距离对整个数组进行排序:

usort($result['businesses'], function ($a, $b){
    return $a['distance'] - $b['distance'] ;
});

Now you have a list of businesses of different categories, with the closest always in first position, whatever its category is. 现在,您有了一个不同类别的企业列表,无论类别是什么,最接近的企业始终排在第一位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM