从API响应中检索数据点

时间:2017-09-06 21:23:46

标签: php

我拼凑了一些PHP代码,允许我从Yelp API v.3

获取业务数据

$postData = "grant_type=client_credentials&".
    "client_id=MyClientIDl94gqHAg&".
    "client_secret=SomEcoDehIW09e6BGuBi4NlJ43HnnHl4S7W5eoXUkB";


// GET TOKEN
$curl = curl_init();

//set the url
curl_setopt($curl,CURLOPT_URL, "https://api.yelp.com/oauth2/token");
//tell curl we are doing a post
curl_setopt($curl,CURLOPT_POST, TRUE);
//set post fields
curl_setopt($curl,CURLOPT_POSTFIELDS, $postData);
//tell curl we want the returned data
curl_setopt($curl,CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($curl);

if($result){
    $data = json_decode($result);
}

// GET RESTAURANT INFO
curl_setopt_array($curl, array(
    CURLOPT_URL => "https://api.yelp.com/v3/businesses/north-india-restaurant-san-francisco",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_HTTPHEADER => array(
        "authorization: Bearer ".$data->access_token
    ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

//close connection
curl_close($curl);

if ($err) {
    echo "cURL Error #:" . $err;
} else {
    echo $response;
}

输出如下:

  

{“id”:“north-india-restaurant-san-francisco”,“name”:“North India Restaurant”,“image_url”:“https://s3-media2.fl.yelpcdn.com/bphoto/fIgAIMHGfWRF3I0JmagQ7A/o.jpg”,“is_claimed”:true,“is_closed “:false,”url“:”https://www.yelp.com/biz/north-india-restaurant-san-francisco?adjust_creative=qG_bVdazAjjAO1l94gqHAg&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_lookup&utm_source=qG_bVdazAjjAO1l94gqHAg“,”phone“:”+ 14153481234“,”display_phone“:”(415)348-1234“,”review_count“:857,”categories“:[{ “别名”:“indpak”,“title”:“Indian”}],“rating”:4.0,“location”:{“address1”:“123 Second St”,“address2”:“”,“address3”: “”,“city”:“旧金山”,“zip_code”:“94105”,“country”:“US”,“state”:“CA”,“display_address”:[“123 Second St”,“旧金山,CA 94105“],”cross_streets“:”“},”coordinates“:{”纬度“:37.787789124691,”经度“:-122.399305736113},”照片“:[”https://s3-media1.fl.yelpcdn.com/bphoto/howYvOKNPXU9A5KUahEXLA/o.jpg“,”{{3 }“,”https://s3-media2.fl.yelpcdn.com/bphoto/I-CX8nioj3_ybAAYmhZcYg/o.jpg“],”价格“:”$$“,”小时“:[{”打开“:[{”is_overnight“:false,”start“:”1000“,”end“ “:”2300“,”day“:0},{”is_overnight“:false,”start“:”1000“,”end“:”2300“,”day“:1},{”is_overnight“:false, “start”:“1000”,“end”:“2300”,“day”:2},{“is_overnight”:false,“start”:“1000”,“end”:“2300”,“day”: 3},{“is_overnight”:false,“st art“:”1000“,”end“:”0000“,”day“:4},{”is_overnight“:false,”start“:”1000“,”end“:”0000“,”day“:5 },{“is_overnight”:false,“start”:“1000”,“end”:“2300”,“day”:6}],“hours_type”:“REGULAR”,“is_open_now”:true}],“交易“:[”交付“,”取件“]}

例如,如何在星期五检索开放时间?这一天是周一至周日的0-6。

1 个答案:

答案 0 :(得分:0)

您解析json数据并迭代条目:

function getOpeningHoursForDay($data,$day){
    $open_hours = $data['hours'][0]['open'];
    $n = count($open_hours);

    for($i=0;$i<$n;$i++){
        if($open_hours[$i]['day']==$day) // check if day matches
            return $open_hours[$i];
    }
    return null; // no matching day found, return null
}

function formatOpeningHour($hour){
    return substr($hour,0,2).':'.substr($hour,2); // convert 2300 to 23:00
}


$data = json_decode($response,true);
$friday_hours = getOpeningHoursForDay($data,4);
if(isset($friday_hours)){
    echo 'opened ' . formatOpeningHour($friday_hours['start']) . ' - ' .formatOpeningHour($friday_hours['end']);
}else{
    echo 'closed';
}