예제 #1
0
<?php

require "wx_config.php";
// require weixin and database config file
$wechatObj = new wechatCallbackAPI();
if ($_SERVER["REMOTE_ADDR"] == "127.0.0.1") {
    $wechatObj->upload_temp_image();
}
exit;
class wechatCallbackAPI
{
    // identity authentication
    private function get_access_token()
    {
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . AppID . "&secret=" . AppSecret;
        $data = json_decode(file_get_contents($url), true);
        if ($data['access_token']) {
            return $data['access_token'];
        } else {
            exit;
        }
    }
    // upload temporary image
    public function upload_temp_image()
    {
        $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=" . $this->get_access_token() . "&type=image";
        $imgAry = scandir("images");
        // scan downloaded images path
        $countImg = count($imgAry);
        $conn = mysql_connect(MySQL_Host, MySQL_User, MySQL_Password);
        if (!$conn) {
예제 #2
0
<?php

require "wx_config.php";
// require weixin and database config file
$wechatObj = new wechatCallbackAPI();
if (!empty($_GET['echostr'])) {
    $wechatObj->valid();
} else {
    $wechatObj->responseMsg();
}
exit;
class wechatCallbackAPI
{
    // identity authentication
    public function valid()
    {
        $echoStr = $_GET["echostr"];
        if ($this->checkSignature()) {
            echo $echoStr;
        }
        exit;
    }
    // check signature for weixin server
    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        $tmpArr = array(TOKEN, $timestamp, $nonce);
        sort($tmpArr);
        $tmpStr = implode($tmpArr);