tips-1068202-00.PHP index.php responseMsg()//$wechat-valid();//Token验证? tips-1068202-01.PHP wechat.inc.php _appid = $appid; $this-_appsecret = $appsecret; $this-_token = $token; } private function _request($curl, $https = true, $post =
index.php responseMsg() //$wechat->valid();//Token验证 ?>tips-1068202-01.PHP
wechat.inc.php
_appid = $appid;
$this->_appsecret = $appsecret;
$this->_token = $token;
}
private function _request($curl, $https = true, $post = "get", $data = null, $header = null, $type = null)
{
$Curl = new Curl();
return $Curl->_request($curl, $https, $post, $data, $header, $type);
}
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature())
{
echo $echoStr;
exit;
}
}
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = $this->_token;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature )
{
return true;
}
else
{
return false;
}
}
private function _getAccessToken()
{
$file = "./accessToken";
if (file_exists($file))
{
$content = file_get_contents($file);
$content = json_decode($content);
if (time() - filemtime($file) < $content->expires_in)
{
return $content->access_token;
}
}
$content = $this->_request("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->_appid."&secret=".$this->_appsecret);
file_put_contents($file, $content);
$content = json_decode($content);
return $content->access_token;
}
public function responseMsg()
{
//get post data, May be due to the different environments
$postStr = file_get_contents("php://input");
//extract post data
if (!empty($postStr))
{
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
if (!empty($postObj))
{
switch($postObj->MsgType)
{
case 'text':
$this->_doText($postObj);
break;
default: exit;
}
}
}
else
{
echo "";
exit;
}
}
private function _doText($postObj)
{
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "
%s
%s
%s
%s
%s
0
";
if (!empty( $keyword ))
{
if ($keyword == "help")
{
$contentStr = "Please input the weekly report content.";
}
else
{
require "./kintone.php";
$kintone = new Kintone();
$result = $kintone->addData($keyword);
}
$msgType = "text";
if ($result == '')
{
$contentStr = "Added!";
}
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
exit;
}
}
?>
tips-1068202-02.PHP
kintone.php
_subDomain . ".cybozu.com:443",
"Content-Type: application/json",
"X-Cybozu-API-Token: " . $this->_apiToken
);
$url = "https://" . $this->_subDomain . ".cybozu.com/k/v1/record.json";
$record = array( "创建人" => array( "value" => array("code"=>$this->_createCode) ),
"日期" => array( "value" => date("Y-m-d") ),
"内容" => array( "value" => $content ));
$data = array( "app" => $this->_appId, "record" => $record );
$response = $this->_request($url, true, "post", json_encode($data), $header);
$response = json_decode($response);
if ($response->version)
{
return $response->id;
}
return $response->message;
}
private function _request($curl, $https = true, $post = "get", $data = null, $header = null, $type = null)
{
$Curl = new Curl();
return $Curl->_request($curl, $https, $post, $data, $header, $type);
}
}
?>
tips-1068202-03.PHP
curl.php
