溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

php的菜譜大全api調(diào)用代碼實(shí)例

發(fā)布時間:2021-06-26 09:16:30 來源:億速云 閱讀:188 作者:chen 欄目:編程語言

本篇內(nèi)容主要講解“php的菜譜大全api調(diào)用代碼實(shí)例”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“php的菜譜大全api調(diào)用代碼實(shí)例”吧!

接口地址:http://www.juhe.cn/docs/api/id/46

// +----------------------------------------------------------------------

//----------------------------------

// 菜譜大全調(diào)用示例代碼 - 聚合數(shù)據(jù)

// 在線接口文檔: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,//應(yīng)用APPKEY(應(yīng)用詳細(xì)頁查詢)

"dtype" => "",//返回?cái)?shù)據(jù)的格式,xml或json,默認(rèn)json

"pn" => "",//數(shù)據(jù)返回起始下標(biāo)

"rn" => "",//數(shù)據(jù)返回條數(shù),最大30

"albums" => "",//albums字段類型,1字符串,默認(rèn)數(shù)組

);

$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.分類標(biāo)簽列表************

$url = "http://apis.juhe.cn/cook/category";

$params = array(

"parentid" => "",//分類ID,默認(rèn)全部

"key" => $appkey,//應(yīng)用APPKEY(應(yīng)用詳細(xì)頁查詢)

"dtype" => "",//返回?cái)?shù)據(jù)的格式,xml或json,默認(rèn)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.按標(biāo)簽檢索菜譜************

$url = "http://apis.juhe.cn/cook/index";

$params = array(

"cid" => "",//標(biāo)簽ID

"key" => $appkey,//應(yīng)用APPKEY(應(yīng)用詳細(xì)頁查詢)

"dtype" => "",//返回?cái)?shù)據(jù)的格式,xml或json,默認(rèn)json

"pn" => "",//數(shù)據(jù)返回起始下標(biāo),默認(rèn)0

"rn" => "",//數(shù)據(jù)返回條數(shù),最大30,默認(rèn)10

"format" => "",//steps字段屏蔽,默認(rèn)顯示,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查看詳細(xì)************

$url = "http://apis.juhe.cn/cook/queryid";

$params = array(

"id" => "",//菜譜的ID

"key" => $appkey,//應(yīng)用APPKEY(應(yīng)用詳細(xì)頁查詢)

"dtype" => "",//返回?cái)?shù)據(jù)的格式,xml或json,默認(rèn)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 "請求失敗";

}

//**************************************************

/**

* 請求接口返回內(nèi)容

* @param string $url [請求的URL地址]

* @param string $params [請求的參數(shù)]

* @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調(diào)用代碼實(shí)例”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI