Search code examples
phpjsonyelp

I need to get information from JSON in PHP


Here is the result of: var_dump($response):

"is_claimed": false, "rating": 4.5, "mobile_url": "http://m.yelp.com/biz/filbert-steps-san-francisco?utm_campaign=yelp_api\u0026utm_medium=api_v2_business\u0026utm_source=NUQkLT4j4VnC6ZR7LI-VWA", "rating_img_url": "https://s3-media2.fl.yelpcdn.com/assets/2/www/img/99493c12711e/ico/stars/v1/stars_4_half.png", "review_count": 208

I want to get the rating value, I tried $response->rating but I got nothing.


Solution

  • You need to make this json first by using {} at two sides of the string. After decoding (json_decode) you will got an Array of Objects.

    $json = '{"is_claimed": false, "rating": 4.5, "mobile_url": "http://m.yelp.com/biz/filbert-steps-san-francisco?utm_campaign=yelp_api\\u0026utm_medium=api_v2_business\\u0026utm_source=NUQkLT4j4VnC6ZR7LI-VWA", "rating_img_url": "https://s3-media2.fl.yelpcdn.com/assets/2/www/img/99493c12711e/ico/stars/v1/stars_4_half.png", "review_count": 208}';
    $result = json_decode ($json);
    
    echo $result->rating; // 4.5
    

    Online Check, and let me know is it works for you or not.