【问题标题】:Yelp API v3 search by phoneYelp API v3 电话搜索
【发布时间】:2019-02-09 20:06:31
【问题描述】:

我需要能够通过电话找到业务。我找到了一个执行搜索的代码示例并且效果很好,但是当我将 myurl 更改为通过电话搜索时 - 即使我使用通过搜索找到的电话也不会返回任何结果...

我错过了什么?

<div id="results"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script>
var myurl = "https://cors-anywhere.herokuapp.com/https://api.yelp.com/v3/businesses/search?term=sushi&location=austin";
// var myurl = "https://cors-anywhere.herokuapp.com/https://api.yelp.com/v3/businesses/phone?phone=+15122750852";

$.ajax({
    url: myurl,
    headers: {
        'Authorization':'Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    },
    method: 'GET',
    dataType: 'json',
    success: function(data){
        // Grab the results from the API JSON return
        var totalresults = data.total;
        // If our results are greater than 0, continue
        if (totalresults > 0){
            // Display a header on the page with the number of results
            $('#results').append('<h5>We discovered ' + totalresults + ' results!</h5>');
            // Itirate through the JSON array of 'businesses' which was returned by the API
            $.each(data.businesses, function(i, item) {
                // Store each business's object in a variable
                var id = item.id;
                var alias = item.alias;
                var phone = item.display_phone;
                var image = item.image_url;
                var name = item.name;
                var rating = item.rating;
                var reviewcount = item.review_count;
                var address = item.location.address1;
                var city = item.location.city;
                var state = item.location.state;
                var zipcode = item.location.zip_code;
                // Append our result into our page
                $('#results').append('<div id="' + id + '" style="margin-top:50px;margin-bottom:50px;"><img src="' + image + '" style="width:200px;height:150px;"><br>We found <b>' + name + '</b> (' + alias + ')<br>Business ID: ' + id + '<br> Located at: ' + address + ' ' + city + ', ' + state + ' ' + zipcode + '<br>The phone number for this business is: ' + phone + '<br>This business has a rating of ' + rating + ' with ' + reviewcount + ' reviews.</div>');
            });
        } else {
            // If our results are 0; no businesses were returned by the JSON therefor we display on the page no results were found
            $('#results').append('<h5>We discovered no results!</h5>');
        }
    }
});

</script>

【问题讨论】:

    标签: jquery api yelp yelp-fusion-api


    【解决方案1】:

    我认为 V3 的端点已关闭;我已经更新了你的代码。请尝试以下方法:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    
    <div id="results"></div>
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
    <script>
    //var myurl = "https://cors-anywhere.herokuapp.com/https://api.yelp.com/v3/businesses/search?term=sushi&location=austin";
    var myurl = "https://cors-anywhere.herokuapp.com/https://api.yelp.com/v3/businesses/search/phone?phone=+15122750852";
    
    $.ajax({
        url: myurl,
        headers: {
            'Authorization':'Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
        },
        method: 'GET',
        dataType: 'json',
        success: function(data){
            // Grab the results from the API JSON return
            var totalresults = data.total;
            // If our results are greater than 0, continue
            if (totalresults > 0){
                // Display a header on the page with the number of results
                $('#results').append('<h5>We discovered ' + totalresults + ' results!</h5>');
                // Itirate through the JSON array of 'businesses' which was returned by the API
                $.each(data.businesses, function(i, item) {
                    // Store each business's object in a variable
                    var id = item.id;
                    var alias = item.alias;
                    var phone = item.display_phone;
                    var image = item.image_url;
                    var name = item.name;
                    var rating = item.rating;
                    var reviewcount = item.review_count;
                    var address = item.location.address1;
                    var city = item.location.city;
                    var state = item.location.state;
                    var zipcode = item.location.zip_code;
                    // Append our result into our page
                    $('#results').append('<div id="' + id + '" style="margin-top:50px;margin-bottom:50px;"><img src="' + image + '" style="width:200px;height:150px;"><br>We found <b>' + name + '</b> (' + alias + ')<br>Business ID: ' + id + '<br> Located at: ' + address + ' ' + city + ', ' + state + ' ' + zipcode + '<br>The phone number for this business is: ' + phone + '<br>This business has a rating of ' + rating + ' with ' + reviewcount + ' reviews.</div>');
                });
            } else {
                // If our results are 0; no businesses were returned by the JSON therefor we display on the page no results were found
                $('#results').append('<h5>We discovered no results!</h5>');
            }
        }
    });
    
    </script>
    
    
    </body>
    </html>
    

    别忘了更新您应用的 API 密钥。

    你的 URI:https://api.yelp.com/v3/businesses/phone?phone=+15122750852 应该是 https://api.yelp.com/v3/businesses/search/phone?phone=+15122750852;您的代码中发生的事情是您因缺少服务而陷入死胡同。 :)

    【讨论】:

    • 太棒了!是的,这就是问题所在。我希望 Yelp 实际上会添加一些代码示例,而不是编写代码……还有一个问题,返回数据似乎有几秒钟的延迟。是因为代理连接吗?有什么办法可以加快速度吗?谢谢。
    • 欢迎,开发人员文档(在您的商务电话搜索的情况下)实际上具有服务的 GET 端点(包括要发送的内容和期望的内容),如下所示:yelp.com/developers/documentation/v3/business_search_phone 实际上你的代码写得很好;它只是一个被误解的服务 URL;它发生了 :) 祝你编码好运,玩得开心!
    • 啊,忘记回答你的问题了;由于 Yelps 响应时间 + 用于绕过 Ajax 中的 CORS 的 CORS 随处 API,您的查询缓慢;您可以在此期间显示加载屏幕或用 PHP 编写自己的桥接器以获得更快的结果并 Ajax 您自己的服务器(CORS 不会成为问题)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多