亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

php微信開發(fā)之百度天氣預(yù)報(bào)

 更新時(shí)間:2016年11月18日 11:14:06   作者:u014427391  
這篇文章主要為大家詳細(xì)介紹了php微信開發(fā)之百度天氣預(yù)報(bào)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了php微信百度天氣預(yù)報(bào)的開發(fā)代碼,供大家參考,具體內(nèi)容如下

1.登錄百度ak申請(qǐng):http://lbsyun.baidu.com/apiconsole/key


2.實(shí)現(xiàn)天氣信息功能

baiduWeather.php 

<?php 
/** 
 * 使用百度天氣預(yù)報(bào)接口獲取城市天氣信息案例實(shí)現(xiàn) 
 */ 
 
 //獲取城市天氣信息 
 function getWeatherInfo($cityName){ 
  if($cityName == "" || (strstr($cityName,"+"))){ 
   return "發(fā)送城市加天氣,例如北京天氣"; 
  } 
  //獲取到的ak 
  $ak = your ak; 
  //獲取到的sk 
  $sk = your sk; 
  //調(diào)用接口 
  $url = 'http://api.map.baidu.com/telematics/v3/weather?ak=%s&location=%s&output=%s&sk=%s'; 
  $uri = '/telematics/v3/weather'; 
 
  $location = $cityName; 
  $output = 'json'; 
  $querystring_arrays = array( 
   'ak' => $ak, 
   'location' => $location, 
   'output' => $output 
  ); 
 
  $querystring = http_build_query($querystring_arrays); 
  //生成sn 
  $sn = md5(urlencode($uri.'?'.$querystring.$sk)); 
  $targetUrl = sprintf($url,$ak,urlencode($location),$output,$sn); 
 
  $ch = curl_init(); 
  curl_setopt($ch,CURLOPT_URL,$targetUrl); 
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
  $result = curl_exec($ch); 
  curl_close($ch); 
  $result = json_decode($result,true); 
 
  if($result["error"]!=0){ 
   return $result["status"]; 
  } 
 
  $curHour = (int)date('H',time()); 
  $weather = $result["results"][0]; 
  $weatherArray[]=array("Title"=>$weather['currentCity']."天氣預(yù)報(bào)","Description"=>"","PicUrl"=>"","Url"=>""); 
  for($i = 0;$i<count($weather["weather_data"]);$i++){ 
   $weatherArray[] = array("Title"=> 
    $weather["weather_data"][$i]["data"]."\n". 
    $weather["weather_data"][$i]["weather"]. 
    $weather["weather_data"][$i]["wind"]. 
    $weather["weather_data"][$i]["temperature"], 
    "Description"=>"", 
    "PicUrl"=>(($curHour>=6)&&($curHour< 
    18))?$weather["weather_data"][$i]["dayPictureUrl"]:$weather["weather_data"][$i]["nightPictureUrl"],"URL"=>"" 
   ); 
  } 
  return $weatherArray; 
 } 

3.實(shí)現(xiàn)天氣消息事件

<?php 
/* 
 CopyRight 2016 All Rights Reserved 
*/ 
 
define("TOKEN", "weixin"); 
/** 
 * 百度天氣預(yù)報(bào)案例實(shí)現(xiàn) 
 * 實(shí)現(xiàn)思路: 
 * 1.申請(qǐng)百度ak、sk 
 * 2.使用百度天氣預(yù)報(bào)接口 
 * 3.實(shí)現(xiàn)天氣信息功能 
 * 4.實(shí)現(xiàn)事件響應(yīng)功能 
 */ 
$wechatObj = new wechatCallbackapiTest(); 
if (!isset($_GET['echostr'])) { 
 $wechatObj->responseMsg(); 
}else{ 
 $wechatObj->valid(); 
} 
 
class wechatCallbackapiTest 
{ 
 //驗(yàn)證簽名 
 public function valid() 
 { 
  $echoStr = $_GET["echostr"]; 
  if($this->checkSignature()){ 
   header('content-type:text'); 
   echo $echoStr; 
   exit; 
  } 
 } 
 
 public function checkSignature(){ 
  $signature = $_GET["signature"]; 
  $timestamp = $_GET["timestamp"]; 
  $nonce = $_GET["nonce"]; 
  $token = TOKEN; 
  $tmpArr = array($token, $timestamp, $nonce); 
  sort($tmpArr); 
  $tmpStr = implode($tmpArr); 
  $tmpStr = sha1($tmpStr); 
  if($tmpStr == $signature) { 
   return true; 
  }else{ 
   return false; 
  } 
 } 
 
 //響應(yīng)消息 
 public function responseMsg() 
 { 
  $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; 
  if (!empty($postStr)){ 
   $this->logger("R ".$postStr); 
   $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); 
   $RX_TYPE = trim($postObj->MsgType); 
 
   //消息類型分離 
   switch ($RX_TYPE) 
   { 
    case "event": 
     $result = $this->receiveEvent($postObj); 
     break; 
    case "text": 
     $result = $this->receiveText($postObj); 
     break; 
    default: 
     $result = "unknown msg type: ".$RX_TYPE; 
     break; 
   } 
   echo $result; 
  }else { 
   echo ""; 
   exit; 
  } 
 } 
 
 //接收事件消息 
 public function receiveEvent($object) 
 { 
  $content = ""; 
  switch ($object->Event) 
  { 
   case "subscribe": 
    $content = "歡迎關(guān)注Nicky的公眾號(hào) "; 
    $content .= (!empty($object->EventKey))?("\n來自二維碼場(chǎng)景 ".str_replace("qrscene_","",$object->EventKey)):""; 
    break; 
   case "unsubscribe": 
    $content = "取消關(guān)注"; 
    break; 
  } 
  $result = $this->transmitText($object, $content); 
  return $result; 
 } 
 
 //接收文本消息 
 public function receiveText($object) 
 { 
  $keyword = trim($object->Content); 
 
  //自動(dòng)回復(fù)模式 
 
  if (strstr($keyword, "天氣")){ 
   $city = str_replace('天氣','',$keyword); 
   include("baiduweather.php"); 
   $content = getWeatherInfo($city); 
  } 
  $result = $this->transmitNews($object, $content); 
  return $result; 
 } 
 
 //回復(fù)圖文消息 
 public function transmitNews($object, $newsArray) 
 { 
  if(!is_array($newsArray)){ 
   return; 
  } 
  $itemTpl = " <item> 
  <Title><![CDATA[%s]]></Title> 
  <Description><![CDATA[%s]]></Description> 
  <PicUrl><![CDATA[%s]]></PicUrl> 
  <Url><![CDATA[%s]]></Url> 
 </item> 
"; 
  $item_str = ""; 
  foreach ($newsArray as $item){ 
   $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']); 
  } 
  $xmlTpl = "<xml> 
<ToUserName><![CDATA[%s]]></ToUserName> 
<FromUserName><![CDATA[%s]]></FromUserName> 
<CreateTime>%s</CreateTime> 
<MsgType><![CDATA[news]]></MsgType> 
<ArticleCount>%s</ArticleCount> 
<Articles> 
$item_str</Articles> 
</xml>"; 
 
  $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray)); 
  return $result; 
 } 
 
 
 //日志記錄 
 public function logger($log_content) 
 { 
  if(isset($_SERVER['HTTP_APPNAME'])){ //SAE 
   sae_set_display_errors(false); 
   sae_debug($log_content); 
   sae_set_display_errors(true); 
  }else if($_SERVER['REMOTE_ADDR'] != "127.0.0.1"){ //LOCAL 
   $max_size = 10000; 
   $log_filename = "log.xml"; 
   if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);} 
   file_put_contents($log_filename, date('H:i:s')." ".$log_content."\r\n", FILE_APPEND); 
  } 
 } 
 
} 
?> 

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Laravel框架源碼解析之模型Model原理與用法解析

    Laravel框架源碼解析之模型Model原理與用法解析

    這篇文章主要介紹了Laravel框架源碼解析之模型Model,結(jié)合實(shí)例形式分析了Laravel框架模型Model相關(guān)原理、用法及操作注意事項(xiàng),需要的朋友可以參考下
    2020-05-05
  • Laravel使用支付寶進(jìn)行支付的示例代碼

    Laravel使用支付寶進(jìn)行支付的示例代碼

    本篇文章主要介紹了Laravel使用支付寶進(jìn)行支付的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • php使用cookie實(shí)現(xiàn)記住登錄狀態(tài)

    php使用cookie實(shí)現(xiàn)記住登錄狀態(tài)

    這篇文章主要介紹了php使用cookie實(shí)現(xiàn)記住登錄狀態(tài),本文用最原始的方法講解如何實(shí)現(xiàn)記住登錄狀態(tài),給出3個(gè)步驟和具體實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2015-04-04
  • PHP抓取網(wǎng)頁、解析HTML常用的方法總結(jié)

    PHP抓取網(wǎng)頁、解析HTML常用的方法總結(jié)

    這篇文章主要介紹了PHP抓取網(wǎng)頁、解析HTML常用的方法總結(jié),本文只是對(duì)可以實(shí)現(xiàn)這兩個(gè)需求的方法作了總結(jié),只介紹方法,不介紹如何實(shí)現(xiàn),需要的朋友可以參考下
    2015-07-07
  • thinkphp5.1框架中容器(Container)和門面(Facade)的實(shí)現(xiàn)方法分析

    thinkphp5.1框架中容器(Container)和門面(Facade)的實(shí)現(xiàn)方法分析

    這篇文章主要介紹了thinkphp5.1框架中容器(Container)和門面(Facade)的實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了thinkPHP5.1框架中容器與門面的定義、實(shí)現(xiàn)方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2019-08-08
  • PHP中使用TCPDF生成PDF文檔實(shí)例

    PHP中使用TCPDF生成PDF文檔實(shí)例

    這篇文章主要介紹了PHP中使用TCPDF生成PDF文檔實(shí)例,文中還介紹了其它常用的PHP生成PDF開源項(xiàng)目,需要的朋友可以參考下
    2014-07-07
  • php讀取本地json文件的實(shí)例

    php讀取本地json文件的實(shí)例

    下面小編就為大家分享一篇php讀取本地json文件的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • Symfony2實(shí)現(xiàn)在controller中獲取url的方法

    Symfony2實(shí)現(xiàn)在controller中獲取url的方法

    這篇文章主要介紹了Symfony2實(shí)現(xiàn)在controller中獲取url的方法,實(shí)例分析了Symfony獲取URL的常用方法與使用技巧,需要的朋友可以參考下
    2016-03-03
  • ThinkPHP3.1.2 使用cli命令行模式運(yùn)行的方法

    ThinkPHP3.1.2 使用cli命令行模式運(yùn)行的方法

    這篇文章主要介紹了ThinkPHP3.1.2 使用cli命令行模式運(yùn)行的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-04-04
  • 帝國cms常用標(biāo)簽匯總

    帝國cms常用標(biāo)簽匯總

    這篇文章主要給大家匯總介紹了一些帝國cms常用標(biāo)簽以及使用小技巧,非常的實(shí)用,這里推薦給大家。
    2015-07-07

最新評(píng)論