php+CI 接入微信公众平台开发, 填写服务器配置验证Token

class Api extends CI_Controller {

public function __construct()

{

parent::__construct();

}

//页面默认方法

public function index()

{

define("TOKEN", "weixin");

$this->valid();

}

public function valid()

{

$echoStr = $_GET["echostr"];

//valid signature , option

if($this->checkSignature())

{

echo $echoStr;

exit;

}

}

private function checkSignature()

{

// you must define TOKEN by yourself

if (!defined("TOKEN")) {

throw new Exception('TOKEN is not defined!');

}

$signature = $_GET["signature"];

$timestamp = $_GET["timestamp"];

$nonce = $_GET["nonce"];

$token = "q2w3e4r5t";

$tmpArr = array($token, $timestamp, $nonce);

// use SORT_STRING rule

sort($tmpArr, SORT_STRING);

$tmpStr = implode( $tmpArr );

$tmpStr = sha1( $tmpStr );

if( $tmpStr == $signature ){

return true;

}else{

return false;

}

}

/**

* 记录日志

* @param $str

* @param $mode

*/

public function write_log($str){

$mode='a';//追加方式写

$file = "Apilog.txt";

$oldmask = @umask(0);

$fp = @fopen($file,$mode);

@flock($fp, 3);

if(!$fp)

{

Return false;

}

else

{

@fwrite($fp,$str);

@fclose($fp);

@umask($oldmask);

Return true;

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容