Home > Backend Development > PHP Tutorial > Public bicycle network API calling code example based on PHP

Public bicycle network API calling code example based on PHP

WBOY
Release: 2016-07-25 08:44:00
Original
1432 people have browsed it
Code description: PHP-based public bicycle network API calling code example
Interface address: http://www.juhe.cn/docs/api/id/41
  1. // +------------- -------------------------------------------------- --------
  2. //----------------------------------
  3. // Real time Public bicycle calling sample code - aggregated data
  4. // Online interface documentation: http://www.juhe.cn/docs/41
  5. //------------------- ---------------
  6. header('Content-type:text/html;charset=utf-8');
  7. //Configure the appkey you applied for
  8. $appkey = "**********************";
  9. //************1.Suzhou**** ********
  10. $url = "http://web.juhe.cn:8080/bike/state/sz";
  11. $params = array(
  12. "state" => "",// Site name, such as: "Times Square"
  13. "key" => $appkey,// APP Key
  14. );
  15. $paramstring = http_build_query($params);
  16. $content = juhecurl($url,$paramstring);
  17. $result = json_decode($content,true);
  18. if($result){
  19. if($result['error_code']=='0'){
  20. print_r($result);
  21. }else{
  22. echo $result ['error_code'].":".$result['reason'];
  23. }
  24. }else{
  25. echo "Request failed";
  26. }
  27. //************** ************************************
  28. //******* *****2.Changshu************
  29. $url = "http://web.juhe.cn:8080/bike/state/cs";
  30. $params = array(
  31. "state" => "",//Site name, such as: "Yushan Park Entrance North"
  32. "key" => $appkey,// APP Key
  33. );
  34. $paramstring = http_build_query($params) ;
  35. $content = juhecurl($url,$paramstring);
  36. $result = json_decode($content,true);
  37. if($result){
  38. if($result['error_code']=='0'){
  39. print_r($result);
  40. }else{
  41. echo $result['error_code'].":".$result['reason'];
  42. }
  43. }else{
  44. echo "Request failed";
  45. }
  46. / /************************************************ *
  47. //************3.Kunshan************
  48. $url = "http://web.juhe.cn: 8080/bike/state/ks";
  49. $params = array(
  50. "state" => "",//Site name, such as: "Sports Center (North)"
  51. "key" => $appkey,/ / APP Key
  52. );
  53. $paramstring = http_build_query($params);
  54. $content = juhecurl($url,$paramstring);
  55. $result = json_decode($content,true);
  56. if($result){
  57. if ($result['error_code']=='0'){
  58. print_r($result);
  59. }else{
  60. echo $result['error_code'].":".$result['reason'];
  61. }
  62. }else{
  63. echo "Request failed";
  64. }
  65. //************************************ ****************
  66. //************4.Wujiang************
  67. $url = "http://web.juhe.cn:8080/bike/state/wj";
  68. $params = array(
  69. "state" => "",//Site name, such as: "Experimental Primary School North Gate"
  70. "key" => $appkey,// APP Key
  71. );
  72. $paramstring = http_build_query($params);
  73. $content = juhecurl($url,$paramstring);
  74. $result = json_decode($ content,true);
  75. if($result){
  76. if($result['error_code']=='0'){
  77. print_r($result);
  78. }else{
  79. echo $result['error_code']. ":".$result['reason'];
  80. }
  81. }else{
  82. echo "Request failed";
  83. }
  84. //******************** ******************************
  85. //************5 .Nantong************
  86. $url = "http://web.juhe.cn:8080/bike/state/nt";
  87. $params = array(
  88. "state" => ; "",//Site name, such as: "Ctrip North Tower"
  89. "key" => $appkey,// APP Key
  90. );
  91. $paramstring = http_build_query($params);
  92. $content = juhecurl($ url,$paramstring);
  93. $result = json_decode($content,true);
  94. if($result){
  95. if($result['error_code']=='0'){
  96. print_r($result);
  97. }else{
  98. echo $result['error_code'].":".$result['reason'];
  99. }
  100. }else{
  101. echo "Request failed";
  102. }
  103. //**************************************************
  104. //************6.Yixing************
  105. $url = "http://web.juhe.cn:8080/bike /state/yx";
  106. $params = array(
  107. "state" => "",//Site name, such as: "Longbei Mountain Forest Park"
  108. "key" => $appkey,// APP Key
  109. );
  110. $paramstring = http_build_query($params);
  111. $content = juhecurl($url,$paramstring);
  112. $result = json_decode($content,true);
  113. if($result){
  114. if($result[ 'error_code']=='0'){
  115. print_r($result);
  116. }else{
  117. echo $result['error_code'].":".$result['reason'];
  118. }
  119. }else{
  120. echo "Request failed";
  121. }
  122. //**************************************** ************
  123. //************7.Xuzhou************
  124. $url = "http://web.juhe.cn:8080/bike/state/xz";
  125. $params = array(
  126. "state" => "",//Site name, such as: "Micro Park"
  127. "key " => $appkey,// APP Key
  128. );
  129. $paramstring = http_build_query($params);
  130. $content = juhecurl($url,$paramstring);
  131. $result = json_decode($content,true);
  132. if($result){
  133. if($result['error_code']=='0'){
  134. print_r($result);
  135. }else{
  136. echo $result['error_code'].":".$result ['reason'];
  137. }
  138. }else{
  139. echo "Request failed";
  140. }
  141. //************************** ************************
  142. //************8.Zhenjiang**** ********
  143. $url = "http://web.juhe.cn:8080/bike/state/zj";
  144. $params = array(
  145. "state" => "",// Site name, such as: "Jiaoshan Park"
  146. "key" => $appkey,// APP Key
  147. );
  148. $paramstring = http_build_query($params);
  149. $content = juhecurl($url,$paramstring);
  150. $result = json_decode($content,true);
  151. if($result){
  152. if($result['error_code']=='0'){
  153. print_r($result);
  154. }else{
  155. echo $ result['error_code'].":".$result['reason'];
  156. }
  157. }else{
  158. echo "Request failed";
  159. }
  160. //****************** *************************************
  161. /**
  162. * Request interface Return content
  163. * @param string $url [requested URL address]
  164. * @param string $params [requested parameters]
  165. * @param int $ipost [whether to use POST form]
  166. * @return string
  167. */
  168. function juhecurl($url,$params=false,$ispost=0){
  169. $httpInfo = array();
  170. $ch = curl_init();
  171. curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
  172. curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );
  173. curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
  174. curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
  175. curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
  176. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  177. if( $ispost )
  178. {
  179. curl_setopt( $ch , CURLOPT_POST , true );
  180. curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
  181. curl_setopt( $ch , CURLOPT_URL , $url );
  182. }
  183. else
  184. {
  185. if($params){
  186. curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
  187. }else{
  188. curl_setopt( $ch , CURLOPT_URL , $url);
  189. }
  190. }
  191. $response = curl_exec( $ch );
  192. if ($response === FALSE) {
  193. //echo "cURL Error: " . curl_error($ch);
  194. return false;
  195. }
  196. $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
  197. $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
  198. curl_close( $ch );
  199. return $response;
  200. }
复制代码
php, api


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template