0

I need to be able to find business by phone. I found an example of code that does the search and it works great, but when I change myurl to search by phone - no results are returned, even when I use phone found by search...

What am I missing?

<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>
brooksrelyt
  • 3,925
  • 5
  • 31
  • 54
santa
  • 12,234
  • 49
  • 155
  • 255

1 Answers1

1

I think your endpoint is off for V3; I've updated your code. Please try the following:

<!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>

Just don't forget to update your app's API key.

Your URI: https://api.yelp.com/v3/businesses/phone?phone=+15122750852 should've really been https://api.yelp.com/v3/businesses/search/phone?phone=+15122750852; what was happening in your code is you hit a dead end endpoint by missing the service. :)

Ilan P
  • 1,602
  • 1
  • 8
  • 13
  • Brilliant! Yes, that was the issue. I wish Yelp would actually add some code examples instead of writing about the code... One more question, there seems a few seconds delay with returning data. Is it due to proxy connection? Is there any way to speed it up? Thanks. – santa Sep 04 '18 at 20:51
  • Welcome, the developer docs (in your case for business phone search) actually have the GET endpoint for the service (including what to send, and what to expect) as seen here: https://www.yelp.com/developers/documentation/v3/business_search_phone you actually had your code written very well; it was simply a misconstrued service URL; it happens :) good luck coding and have fun! – Ilan P Sep 04 '18 at 20:53
  • Ah, forgot to respond to your question; your query is sluggish because of Yelps response time + the CORS anywhere API you’re using to bypass CORS in Ajax; you can display a loading screen in the interim or wrote your own bridge in PHP to gain quicker result and Ajax your own server (where CORS wouldn’t be an issue) – Ilan P Sep 04 '18 at 21:54