当前位置:网站首页>Google ranging for PHP wechat development

Google ranging for PHP wechat development

2022-06-24 11:15:00 PHP Development Engineer

This article is an example for you to share php Specific code of wechat Google ranging , For your reference , The details are as follows

<?php
/**
 * wechat php test
 */

//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
//$wechatObj->valid();
$wechatObj->responseMsg();
class wechatCallbackapiTest
{
  public function valid()
  {
    $echoStr = $_GET["echostr"];

    //valid signature , option
    if($this->checkSignature()){
      echo $echoStr;
      exit;
    }
  }

  public function responseMsg()
  {
    //get post data, May be due to the different environments
    $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

    //extract post data
    if (!empty($postStr)){

        $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
        $fromUsername = $postObj->FromUserName;
        $toUsername = $postObj->ToUserName;
        $type = $postObj->MsgType;
        $customrevent = $postObj->Event;
        $latitude = $postObj->Location_X;
        $longitude = $postObj->Location_Y;
        $keyword = trim($postObj->Content);
        $time = time();
        $textTpl = "<xml>
              <ToUserName><![CDATA[%s]]></ToUserName>
              <FromUserName><![CDATA[%s]]></FromUserName>
              <CreateTime>%s</CreateTime>
              <MsgType><![CDATA[%s]]></MsgType>
              <Content><![CDATA[%s]]></Content>
              <FuncFlag>0</FuncFlag>
              </xml>";       
        switch ($type)
      {  case "event";
        if ($customrevent=="subscribe")
          {$contentStr = '';}
        break;
        case "image";
        $contentStr = " Your picture is great !";
        break;
        case "location";
        $disurl="http://maps.googleapis.com/maps/api/distancematrix/xml?origins={$latitude},{$longitude}&destinations=23.355164,116.681889&mode=walking&language=zh-CN&sensor=false";
      $apistr=file_get_contents($disurl);
      $apiobj=simplexml_load_string($apistr);
      $disobj=$apiobj->row->element->distance->text;
      $durobj=$apiobj->row->element->duration->text;
      $contentStr = " You have an appointment with our company {$disobj} km , Walk around {$durobj}";

        break;
        case "link" ;
        $contentStr = " Your link has a virus !";
        break;
        case "text";
        $weatherurl="http://api.map.baidu.com/telematics/v2/weather?location={$keyword}&ak=1a3cde429f38434f1811a75e1a90310c";
         $apistr=file_get_contents($weatherurl);
         $apiobj=simplexml_load_string($apistr);
         $placeobj=$apiobj->currentCity;// Read city 
         $todayobj=$apiobj->results->result[0]->date;// Read week 
         $weatherobj=$apiobj->results->result[0]->weather;// Reading weather 
         $windobj=$apiobj->results->result[0]->wind;// Read wind 
         $temobj=$apiobj->results->result[0]->temperature;// Reading temperature 
         $contentStr = "{$placeobj}{$todayobj} The weather {$weatherobj}, wind {$windobj}, temperature {$temobj}";
         break;          
      default;
      $contentStr =" This feature has not yet been developed ";  
      }
        $msgType="text";
        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
        echo $resultStr;


    }else {
      echo "";
      exit;
    }
  }

  private 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;
    }
  }
}

?>

The above is the whole content of this paper , I hope it will be helpful for your study , I also hope you can support open source .

Complete example :http://github.crmeb.net/u/defu

come from “ Open source world ” , link :https://ym.baisou.ltd/post/681.html, If you want to reprint , Please indicate the source , Otherwise, the legal liability will be investigated .

原网站

版权声明
本文为[PHP Development Engineer ]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/06/20210604184024062s.html