Jump to content
新域网络技术论坛

微信公众号开发相关API查询


Jamers
 Share

Recommended Posts

最近微信公众号风风火火,需要一些此类PHP开发技术,找了一下基本的API,做了一些简单的测试。测试过程就不写了,直接放上测试代码。

 

http://mp.weixin.qq.com/wiki/0/2e2239fa5f49388d5b5136ecc8e0e440.html

 

 

完成的动作:当公众号接收到发送的消息,取用户资料并将用户昵称及发送的内容反馈给发送者。

Link to comment
Share on other sites


error_reporting(E_ALL);
ini_set('display_errors', '1');
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');

//这里的内容自己根据自己的值修改
define("TOKEN", "TESTTEST");
define("APPID",'APPID');
define("APPSECRET",'APPSECRET');

$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();

class wechatCallbackapiTest
{
public $fn='wx_debug.txt';
public $atoken='atoken.txt';
private $user;

public function valid()
{
$echoStr = '';
if (isset($_GET["echostr"])) $echoStr = $_GET["echostr"];

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

public function responseMsg()
{
//get post data, May be due to the different environments
$postStr = '';
if (isset($GLOBALS["HTTP_RAW_POST_DATA"])) $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;
$keyword = trim($postObj->Content);
//$this->savelog($keyword);
$time = time();
$textTpl = "


%s


";
if(!empty( $keyword ))
{
$this->getInfo($fromUsername);
$msgType = "text";
//$contentStr = "Welcome to wechat world!";
$contentStr = "'{$this->user->nickname}':{$keyword}";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else{
echo "Input something...";
}

}else {
echo "";
exit;
}
}

private function checkSignature()
{
$signature = '';
$timestamp = '';
$nonce = '';
if (isset($_GET["signature"])) $signature = $_GET["signature"];
if (isset($_GET["timestamp"])) $timestamp = $_GET["timestamp"];
if (isset($_GET["nonce"])) $nonce = $_GET["nonce"];

$token = TOKEN;
//$this->savelog($signature.','.$timestamp.','.$nonce.','.$token);

//$tmp=var_export($_GET,true);
//$this->savelog($tmp);
//$this->savelog($GLOBALS["HTTP_RAW_POST_DATA"]);

$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );

if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}

private function savelog($log) {
//保存日志文件
$f = fopen($this->fn,'a+');
fwrite($f,$log."\r\n");
fclose($f);
}
private function saveit($file,$data) {
//保存文件
$f = fopen($file,'w+');
fwrite($f,$data);
fclose($f);
}

private function getInfo($id) {
//取用户基本信息
//https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN

$atoken = $this->get_access_token();

$output = $this->curl_get("https://api.weixin.qq.com/cgi-bin/user/info?access_token={$atoken}&openid={$id}&lang=zh_CN");
$this->savelog($output);
$this->user = json_decode($output);

}

private function get_access_token() {
//取access token
//{"access_token":"8ib9AZ34_3QzYJFbxgS_dnbPuV3_maHUE5KquIDCnVPOJcrj3Nhmd7Fl2Ohy2NT0m9cRZCwDWNbrmoR7hWh5xSTDMasoIa6GILZ8nboxbho","expires_in":7200}
if (file_exists($this->atoken)) {
//有access_token
$data = file_get_contents($this->atoken);
$mp = explode(',',$data);
if (intval($mp[1])>=time()) {
return $mp[0];
}
}
$json = $this->curl_get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".APPSECRET);
//$this->savelog($json);
$d = json_decode($json);
$a = $d->access_token;
$b = time()+$d->expires_in;
$this->saveit($this->atoken,"{$a},{$b}");
return $a;
}

private function curl_get($link) {
//初始化
$ch = curl_init();
//设置选项,包括URL
curl_setopt($ch, CURLOPT_URL, $link);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);

//执行并获取HTML文档内容
$output = curl_exec($ch);
//释放curl句柄
curl_close($ch);
return $output;
}

private function curl_post($url,$data) {
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
// grab URL, and print
$res = curl_exec($ch);
curl_close($ch);
return $res;
}

}

?>

 

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...