您的位置:首页 > 移动开发 > 微信开发

php微信服务端的接口程序

2015-08-03 16:56 931 查看
<?php

/**

 * Created by PhpStorm.

 * User: Administrator

 * Date: 2015/8/3

 * Time: 15:14

 */ 

$token='weixin';//这里的token是在微信接口配置的token

$wechatApi = new WechatApi($token);

if (isset($_GET['echostr'])) {
$wechatApi->valid();

}else{
echo "meiyoushezhi";

}

class WechatApi

{

    private $token;

    public function __construct($token)

    {

        $this->token = $token;

    }

    public function valid()

    {

        $response = false;

        $echoStr = $_GET["echostr"];

        //valid signature , option

        if ($this->checkSignature()) {

            $response = $echoStr;

        }

        echo  $response;

    }

    public function buildSignUrl($apiUrl, $params=array())

    {

        $url = $apiUrl;

        $token = $this->token;

        $timestamp = "13123123";

        $nonce = 'asdad';

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

        sort($tmpArr);

        $tmpStr = implode($tmpArr);

        $signature = sha1($tmpStr);

        $data = array_merge(array('signature' => $signature, 'timestamp' => $timestamp, 'nonce' => $nonce), $params);

        if (strpos($url, '?')) $url .= '&' . http_build_query($data);

        else $url .= '?' . http_build_query($data);

        return $url;

    }

    private function checkSignature()

    {

        $signature = $_GET["signature"];

        $timestamp = $_GET["timestamp"];

        $nonce = $_GET["nonce"];

        $tmpArr = array($this->token, $timestamp, $nonce);

        sort($tmpArr);

        $tmpStr = implode($tmpArr);

        $tmpStr = sha1($tmpStr);

        if ($tmpStr == $signature) {

            return true;

        } else {

            return false;

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: