Php-based peripheral food api call code example code description: php-based peripheral food api call code example
Interface address: http://www.juhe.cn/docs/api/id/45
// + ---------------------------------------------------------------------- // ---------------------------- // Sample code of food call-aggregate data // online interface documentation: http://www.juhe.cn/docs/45//---------------------------------- Header ('content-type: text/html; charset = utf-8 '); // Configure Your applied appkey $ appkey = "*********************"; // ************* 1. search by city ************ $ url =" http://apis.juhe.cn/catering/query Bycity "; $ params = array (" city "=>" ", // city name, for example, Beijing URLencode" page "=>" ", // Current page number, the default value is 1 "pagesize" => "", // Each page is returned. The maximum value is 50 "key" => $ appkey, // The 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 ";} //************************************** * *********** // *************** 2. search for nearby food ************ $ url =" http://apis.juhe.cn/catering/query "; $ Params = array (" lng "=>" ", // latitude and longitude (for example, 121.538123), transfer the coordinate system suitable for google Maps" lat "=> "", // latitude (for example, 31.677132) "radius" => "", // search range, unit: M, default value: 3000 "page" => "", // Current page number, the default value is 1, and the maximum value is 50. "key" => $ appkey, // application APPKEY (application details page query) "dtype" => ", // format of returned data, 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 [requested URL] * @ param string $ params [request parameter] * @ param int $ ipost [whether to use the POST form] * @ return string */function juhecurl ($ url, $ params = false, $ ispost = 0) {$ httpInfo = array (); $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_HTTP _ VERSION, expiration); 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 ($ c H, 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 ;}