您好,登錄后才能下訂單哦!
本篇內容主要講解“php的菜譜大全api調用代碼實例”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“php的菜譜大全api調用代碼實例”吧!
接口地址:http://www.juhe.cn/docs/api/id/46
// +---------------------------------------------------------------------- //---------------------------------- // 菜譜大全調用示例代碼 - 聚合數據 // 在線接口文檔:http://www.juhe.cn/docs/46 //---------------------------------- header('Content-type:text/html;charset=utf-8'); //配置您申請的appkey $appkey = "*********************"; //************1.菜譜大全************ $url = "http://apis.juhe.cn/cook/query.php"; $params = array( "menu" => "",//需要查詢的菜譜名 "key" => $appkey,//應用APPKEY(應用詳細頁查詢) "dtype" => "",//返回數據的格式,xml或json,默認json "pn" => "",//數據返回起始下標 "rn" => "",//數據返回條數,最大30 "albums" => "",//albums字段類型,1字符串,默認數組 ); $paramstring = http_build_query($params); $content = juhecurl($url,$paramstring); $result = json_decode($content,true); if($result){ if($result['error_code']=='0'){ print_r($result); }else{ echo $result['error_code'].":".$result['reason']; } }else{ echo "請求失敗"; } //************************************************** //************2.分類標簽列表************ $url = "http://apis.juhe.cn/cook/category"; $params = array( "parentid" => "",//分類ID,默認全部 "key" => $appkey,//應用APPKEY(應用詳細頁查詢) "dtype" => "",//返回數據的格式,xml或json,默認json ); $paramstring = http_build_query($params); $content = juhecurl($url,$paramstring); $result = json_decode($content,true); if($result){ if($result['error_code']=='0'){ print_r($result); }else{ echo $result['error_code'].":".$result['reason']; } }else{ echo "請求失敗"; } //************************************************** //************3.按標簽檢索菜譜************ $url = "http://apis.juhe.cn/cook/index"; $params = array( "cid" => "",//標簽ID "key" => $appkey,//應用APPKEY(應用詳細頁查詢) "dtype" => "",//返回數據的格式,xml或json,默認json "pn" => "",//數據返回起始下標,默認0 "rn" => "",//數據返回條數,最大30,默認10 "format" => "",//steps字段屏蔽,默認顯示,format=1時屏蔽 ); $paramstring = http_build_query($params); $content = juhecurl($url,$paramstring); $result = json_decode($content,true); if($result){ if($result['error_code']=='0'){ print_r($result); }else{ echo $result['error_code'].":".$result['reason']; } }else{ echo "請求失敗"; } //************************************************** //************4.按菜譜ID查看詳細************ $url = "http://apis.juhe.cn/cook/queryid"; $params = array( "id" => "",//菜譜的ID "key" => $appkey,//應用APPKEY(應用詳細頁查詢) "dtype" => "",//返回數據的格式,xml或json,默認json ); $paramstring = http_build_query($params); $content = juhecurl($url,$paramstring); $result = json_decode($content,true); if($result){ if($result['error_code']=='0'){ print_r($result); }else{ echo $result['error_code'].":".$result['reason']; } }else{ echo "請求失敗"; } //************************************************** /** * 請求接口返回內容 * @param string $url [請求的URL地址] * @param string $params [請求的參數] * @param int $ipost [是否采用POST形式] * @return string */ function juhecurl($url,$params=false,$ispost=0){ $httpInfo = array(); $ch = curl_init(); curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 ); curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' ); curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 ); curl_setopt( $ch, CURLOPT_TIMEOUT , 60); curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true ); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); if( $ispost ) { curl_setopt( $ch , CURLOPT_POST , true ); curl_setopt( $ch , CURLOPT_POSTFIELDS , $params ); curl_setopt( $ch , CURLOPT_URL , $url ); } else { if($params){ curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params ); }else{ curl_setopt( $ch , CURLOPT_URL , $url); } } $response = curl_exec( $ch ); if ($response === FALSE) { //echo "cURL Error: " . curl_error($ch); return false; } $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE ); $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) ); curl_close( $ch ); return $response; }
到此,相信大家對“php的菜譜大全api調用代碼實例”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。