2017-09-06 82 views
1

我拼湊起來的一些PHP代碼,讓我從Yelp的API 3.0版獲得業務數據檢索數據POIN

$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,網址「:」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「:[{」alias「:」indpak「,」title「 「地址」:{「address1」:「123 Second St」,「address2」:「」,「address3」:「」,「city」:「舊金山」,「 zip_code「:」94105「,」country「:」US「,」state「:」CA「,」display_address「:[」123 Second St「,」San Francisco,CA 94105「],」cross_streets「:」「} ,「座標」:{「緯度」:37.787789124691,「經度」:-122.399305736113},「照片」:[「https://s3-media1.fl.yelpcdn.com/bphoto/howYvOKNPXU9A5KUahEXLA/o.jpg」,「https://s3-media2.fl.yelpcdn.com/bphoto/I-CX8nioj3_ybAAYmhZcYg/o.jpg」,「https://s3-media3.fl.yelpcdn.com/bphoto/uaSNfzJUiFDzMeSCwTcs-A/o.jpg」],「價格」:「$$」 :[{「open」:[{「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,」start「:」1000「,」end「 ,「day」:4},{「is_overnight」:false,「star t「:」1000「,」end「:」0000「,」day「:5},{」is_overnight「:false,」start「:」1000「,」end「:」2300「,」day「 }], 「hours_type」: 「常規」, 「is_open_now」:真正}], 「交易」: 「交貨」, 「皮卡」]}

如何取回週五開放時間,例如?星期一至星期日的星期一至星期六。

回答

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'; 
}