[PHP] 基于php的生活指数api调用代码实例 →→→→→进入此内容的聊天室

来自 , 2021-01-03, 写在 PHP, 查看 119 次.
URL http://www.code666.cn/view/0b6ace9e
  1. <!--?php
  2. // +----------------------------------------------------------------------
  3. // | JuhePHP [ NO ZUO NO DIE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2010-2015 http://juhe.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: Juhedata <info@juhe.cn-->
  8. // +----------------------------------------------------------------------
  9.  
  10. //----------------------------------
  11. // 天气预报调用示例代码 - 聚合数据
  12. // 在线接口文档:http://www.juhe.cn/docs/73
  13. //----------------------------------
  14.  
  15. header('Content-type:text/html;charset=utf-8');
  16.  
  17.  
  18. //配置您申请的appkey
  19. $appkey = "*********************";
  20.  
  21.  
  22.  
  23.  
  24. //************1.根据城市查询天气************
  25. $url = "http://op.juhe.cn/onebox/weather/query";
  26. $params = array(
  27.       "cityname" => "",//要查询的城市,如:温州、上海、北京
  28.       "key" => $appkey,//应用APPKEY(应用详细页查询)
  29.       "dtype" => "",//返回数据的格式,xml或json,默认json
  30. );
  31. $paramstring = http_build_query($params);
  32. $content = juhecurl($url,$paramstring);
  33. $result = json_decode($content,true);
  34. if($result){
  35.     if($result['error_code']=='0'){
  36.         print_r($result);
  37.     }else{
  38.         echo $result['error_code'].":".$result['reason'];
  39.     }
  40. }else{
  41.     echo "请求失败";
  42. }
  43. //**************************************************
  44.  
  45.  
  46.  
  47.  
  48.  
  49. /**
  50.  * 请求接口返回内容
  51.  * @param  string $url [请求的URL地址]
  52.  * @param  string $params [请求的参数]
  53.  * @param  int $ipost [是否采用POST形式]
  54.  * @return  string
  55.  */
  56. function juhecurl($url,$params=false,$ispost=0){
  57.     $httpInfo = array();
  58.     $ch = curl_init();
  59.  
  60.     curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
  61.     curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );
  62.     curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
  63.     curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
  64.     curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
  65.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  66.     if( $ispost )
  67.     {
  68.         curl_setopt( $ch , CURLOPT_POST , true );
  69.         curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
  70.         curl_setopt( $ch , CURLOPT_URL , $url );
  71.     }
  72.     else
  73.     {
  74.         if($params){
  75.             curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
  76.         }else{
  77.             curl_setopt( $ch , CURLOPT_URL , $url);
  78.         }
  79.     }
  80.     $response = curl_exec( $ch );
  81.     if ($response === FALSE) {
  82.         //echo "cURL Error: " . curl_error($ch);
  83.         return false;
  84.     }
  85.     $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
  86.     $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
  87.     curl_close( $ch );
  88.     return $response;
  89. }

回复 "基于php的生活指数api调用代码实例"

这儿你可以回复上面这条便签

captcha