Zach Nicodemous
Zach Nicodemous

Reputation: 9487

How to retrieve data items individually from Yelp Business API 2.0?

I want to try and figure out how to take the data that is output from the Yelp Business API 2.0 and actually choose what bits I want to echo, add styling, so on and so forth.

Here is the basic query:

<?php
        require_once ('lib/OAuth.php');

        $unsigned_url = "http://api.yelp.com/v2/business/the-waterboy-sacramento";

        // Set your keys here
        $consumer_key = 'mykey';
        $consumer_secret = 'mysecret';
        $token = 'mytoken';
        $token_secret = 'mytokensecret';

        $token = new OAuthToken($token, $token_secret);

        $consumer = new OAuthConsumer($consumer_key, $consumer_secret);

        $signature_method = new OAuthSignatureMethod_HMAC_SHA1();

        $oauthrequest = OAuthRequest::from_consumer_and_token($consumer, $token, 'GET', $unsigned_url);

        $oauthrequest->sign_request($signature_method, $consumer, $token);

        $signed_url = $oauthrequest->to_url();

        $ch = curl_init($signed_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, 0);

        $data = curl_exec($ch);
        curl_close($ch);

        $response = json_decode($data);
        $json_string = file_get_contents($signed_url);
        $result = json_decode($json_string);

// Print it for debugging
echo '<pre>';
print_r($result);
echo '</pre>';

?>

In the area where the data is being printed, how would I modify this for example, to return only the business name? Or only the business address? Or both items, each encased in its own div?

Is there a simple method such as:

echo $business_name;
echo $business_address;

I am basically just trying to figure out how to take the deluge of output information, which is just in a basic text format without any formatting whatsoever by default, and actually select what I want to display, give each bit a div with its own class, so I can style it and make it pretty.

Currently, print_r returns:

stdClass Object
(
    [is_claimed] => 
    [rating] => 4
    [mobile_url] => http://m.yelp.com/biz/the-waterboy-sacramento
    [rating_img_url] => http://s3-media4.fl.yelpcdn.com/assets/2/www/img/c2f3dd9799a5/ico/stars/v1/stars_4.png
    [review_count] => 485
    [name] => The Waterboy
    [snippet_image_url] => http://s3-media2.fl.yelpcdn.com/photo/yd0M2_X6-S7mg_jFT4KtnQ/ms.jpg
    [rating_img_url_small] => http://s3-media4.fl.yelpcdn.com/assets/2/www/img/f62a5be2f902/ico/stars/v1/stars_small_4.png
    [url] => http://www.yelp.com/biz/the-waterboy-sacramento
    [menu_date_updated] => 1387494198
    [reviews] => Array
        (
            [0] => stdClass Object
                (
                    [rating] => 5
                    [excerpt] => DEFINITE REPEAT

First time here.  Host greeted us, explained restaurant style, etc.  Very nice gentleman, and very knowledgable.

Our server was...
                    [time_created] => 1406526888
                    [rating_image_url] => http://s3-media1.fl.yelpcdn.com/assets/2/www/img/f1def11e4e79/ico/stars/v1/stars_5.png
                    [rating_image_small_url] => http://s3-media1.fl.yelpcdn.com/assets/2/www/img/c7623205d5cd/ico/stars/v1/stars_small_5.png
                    [user] => stdClass Object
                        (
                            [image_url] => http://s3-media2.fl.yelpcdn.com/photo/yd0M2_X6-S7mg_jFT4KtnQ/ms.jpg
                            [id] => HRxSv183RfenlVyFWX4gCw
                            [name] => Randy S.
                        )

                    [rating_image_large_url] => http://s3-media3.fl.yelpcdn.com/assets/2/www/img/22affc4e6c38/ico/stars/v1/stars_large_5.png
                    [id] => OxfuTiiznTQpwaY6zBa7UQ
                )

        )

    [phone] => 9164989891
    [snippet_text] => DEFINITE REPEAT

First time here.  Host greeted us, explained restaurant style, etc.  Very nice gentleman, and very knowledgable.

Our server was...
    [image_url] => http://s3-media4.fl.yelpcdn.com/bphoto/Y4kSp64IBG2M4CGrENIegA/ms.jpg
    [categories] => Array
        (
            [0] => Array
                (
                    [0] => French
                    [1] => french
                )

            [1] => Array
                (
                    [0] => Italian
                    [1] => italian
                )

        )

    [display_phone] => +1-916-498-9891
    [rating_img_url_large] => http://s3-media2.fl.yelpcdn.com/assets/2/www/img/ccf2b76faa2c/ico/stars/v1/stars_large_4.png
    [menu_provider] => single_platform
    [id] => the-waterboy-sacramento
    [is_closed] => 
    [location] => stdClass Object
        (
            [city] => Sacramento
            [display_address] => Array
                (
                    [0] => 2000 Capitol Ave
                    [1] => Downtown
                    [2] => Sacramento, CA 95811
                )

            [geo_accuracy] => 9
            [neighborhoods] => Array
                (
                    [0] => Downtown
                    [1] => Midtown
                )

            [postal_code] => 95811
            [country_code] => US
            [address] => Array
                (
                    [0] => 2000 Capitol Ave
                )

            [coordinate] => stdClass Object
                (
                    [latitude] => 38.573153
                    [longitude] => -121.481208
                )

            [state_code] => CA
        )

)

Upvotes: 2

Views: 1511

Answers (1)

halfer
halfer

Reputation: 20420

You have a PHP object here, which contains a number of public properties you can retrieve yourself (stdClass is the simplest class that PHP can make objects from). For the name, try this:

echo $result->name;

You can probably see from the print_r output that some data items are sensibly nested. For example, if you want the city, it can be retrieved thus:

echo $result->location->city;

Now, some items - such as the address - are stored in arrays within objects. To get the first line of the address, try this:

echo $result->location->display_address[0];

It's worth reading the PHP manual about objects and arrays, to learn more about what they do, and how to access them. The above is just the basics, but will get you started.

If you want the whole address, consider wrapping the display_address property in implode - this will join elements of an array together with a "glue" string, such as a comma. That's also in the manual.

Finally, be careful when rendering data "raw" to HTML - if this data (which you do not control yourself) contains an angle bracket, it will break your website. Thus, you'll need to wrap everything above in htmlspecialchars or htmlentities, thus:

echo htmlentities($result->name);

Upvotes: 4

Related Questions