Php-based recipe Daquan api call code example code description: php-based recipe Daquan api call code example
Interface address: http://www.juhe.cn/docs/api/id/46
PHP code
// + ---------------------------------------------------------------------- // ------------------------------ // Recipe Daquan call sample code-aggregate data // online interface documentation: http://www.juhe.cn/docs/46//---------------------------------- Header ('content-type: text/html; charset = utf-8 '); // Configure Your applied appkey $ appkey = "*********************"; // ************* 1. recipe list ************* $ url =" http://apis.juhe.cn/cook/query.php "; $ Params = array (" menu "=>" ", // The name of the recipe to be queried" key "=> $ appkey, // apply APPKEY (query on the application details page) "dtype" => "", // The format of the returned data, xml or json. the default value is json "pn" => "", // start subscript of data return "rn" => "", // number of data returned results, maximum 30 "albums" => "", // albums field type, 1 string, default array); $ 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 "request failed ";} //************************************** * *********** // *************** 2. category tag list ************ $ url =" http://apis.juhe.cn/cook/category "; $ Params = array (" parentid "=>" ", // Category ID, all" key "=> $ appkey by default, // APPKEY of the application (query on the application details page) "dtype" => "", // returned data format, xml or json, default 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 "request failed ";} //************************************** * *********** // *************** 3. search for recipes by tag ************ $ url =" http://apis.juhe.cn/cook/index "; $ Params = array (" cid "=>" ", // tag ID" key "=> $ appkey, // APPKEY application (query on the application details page) "dtype" => "", // The format of the returned data, xml or json. the default value is json "pn" => ", // The start subscript of the returned data, the default value is 0 "rn" => "", // number of data entries returned. The maximum value is 30. the default value is 10 "format" => ", // The steps field is blocked. the default value is displayed, 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 "request failed ";} //************************************** * *********** // *************** 4. view details by recipe ID ************* $ url =" http://apis.juhe.cn/cook/queryid "; $ Params = array (" id "=>" ", // recipe ID" key "=> $ appkey, // apply APPKEY (query on the application details page) "dtype" => "", // returned data format, xml or json, default 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 "request failed ";} //************************* * ************************/***** Request interface returned content * @ param string $ url [request URL address] * @ param string $ params [request parameters] * @ param int $ ipost [whether to use POST form] * @ 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_CONNECTTIM EOUT, 60); curl_setopt ($ ch, CURLOPT_TIMEOUT, 60); curl_setopt ($ ch, success, 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 ;}