Heim > Backend-Entwicklung > PHP-Tutorial > 基于php的公共自行车网点api调用代码实例

基于php的公共自行车网点api调用代码实例

WBOY
Freigeben: 2016-07-25 08:44:00
Original
1436 Leute haben es durchsucht
代码描述:基于php的公共自行车网点api调用代码实例
接口地址:http://www.juhe.cn/docs/api/id/41
  1. // +----------------------------------------------------------------------
  2. //----------------------------------
  3. // 实时公共自行车调用示例代码 - 聚合数据
  4. // 在线接口文档:http://www.juhe.cn/docs/41
  5. //----------------------------------
  6. header('Content-type:text/html;charset=utf-8');
  7. //配置您申请的appkey
  8. $appkey = "*********************";
  9. //************1.苏州************
  10. $url = "http://web.juhe.cn:8080/bike/state/sz";
  11. $params = array(
  12. "state" => "",//站点名称,如:"时代广场"
  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 "请求失败";
  26. }
  27. //**************************************************
  28. //************2.常熟************
  29. $url = "http://web.juhe.cn:8080/bike/state/cs";
  30. $params = array(
  31. "state" => "",//站点名称,如:"虞山公园入口北"
  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 "请求失败";
  45. }
  46. //**************************************************
  47. //************3.昆山************
  48. $url = "http://web.juhe.cn:8080/bike/state/ks";
  49. $params = array(
  50. "state" => "",//站点名称,如:"体育中心(北)"
  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 "请求失败";
  64. }
  65. //**************************************************
  66. //************4.吴江************
  67. $url = "http://web.juhe.cn:8080/bike/state/wj";
  68. $params = array(
  69. "state" => "",//站点名称,如:"实验小学北门"
  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 "请求失败";
  83. }
  84. //**************************************************
  85. //************5.南通************
  86. $url = "http://web.juhe.cn:8080/bike/state/nt";
  87. $params = array(
  88. "state" => "",//站点名称,如:"携程北楼"
  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 "请求失败";
  102. }
  103. //**************************************************
  104. //************6.宜兴************
  105. $url = "http://web.juhe.cn:8080/bike/state/yx";
  106. $params = array(
  107. "state" => "",//站点名称,如:"龙背山森林公园"
  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 "请求失败";
  121. }
  122. //**************************************************
  123. //************7.徐州************
  124. $url = "http://web.juhe.cn:8080/bike/state/xz";
  125. $params = array(
  126. "state" => "",//站点名称,如:"微园"
  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 "请求失败";
  140. }
  141. //**************************************************
  142. //************8.镇江************
  143. $url = "http://web.juhe.cn:8080/bike/state/zj";
  144. $params = array(
  145. "state" => "",//站点名称,如:"焦山公园"
  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 "请求失败";
  159. }
  160. //**************************************************
  161. /**
  162. * 请求接口返回内容
  163. * @param string $url [请求的URL地址]
  164. * @param string $params [请求的参数]
  165. * @param int $ipost [是否采用POST形式]
  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


Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage