微信接口开发【php代码】

首先准备一个api.php文件 这个文件可以再微信开发平台网站手册里面下载到

需要配置的是token

微信测试地址

https://mp.weixin.qq.com/debug/

<?php
/**
  * wechat php test
  */

//define your token
define("TOKEN", "这里写你的token");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();

class wechatCallbackapiTest
{
	public function valid()
    {
        $echoStr = $_GET["echostr"];

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

    public function responseMsg()
    {
		//get post data, May be due to the different environments
		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

      	//extract post data
		if (!empty($postStr)){
                /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
                   the best way is to check the validity of xml by yourself */
                libxml_disable_entity_loader(true);
              	$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $keyword = trim($postObj->Content);
                $time = time();
                $textTpl = "<xml>
							<ToUserName><![CDATA[%s]]></ToUserName>
							<FromUserName><![CDATA[%s]]></FromUserName>
							<CreateTime>%s</CreateTime>
							<MsgType><![CDATA[%s]]></MsgType>
							<Content><![CDATA[%s]]></Content>
							<FuncFlag>0</FuncFlag>
							</xml>";             
				if(!empty( $keyword ))
                {
              		$msgType = "text";
                	$contentStr = "Welcome to wechat world!";
                	$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                	echo $resultStr;
                }else{
                	echo "Input something...";
                }

        }else {
        	echo "";
        	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 = TOKEN;
		$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;
		}
	}
}

?>

然后配置你的服务器 

输入你设置的token 配置完成就可以进行开发了

首先需要的几个参数

首先你会拥有一个appID和appsecret


然后你需要设置你的接口配置


这里的url填写你的最开始的那个文件路径

token可以随便写

token填到第一个文件里面去

然后保存提交



提交之后便可以开始开发了 这里附上最基本的获取用户头像和用户名的代码

新建一个文件weixin.php


以下有三个参数需要填写

appid填写你的appid

appsecret填写你的appsecret

redirect_uri填写你的回调域名

<meta charset="utf-8">
<?php
$appid='填写你的appid';
$appsecret='填写你的appsecret';
$redirect_uri='填写你的域名地址/weixin.php';
$code=isset($_GET['code'])?$_GET['code']:'';

$url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';

//获取html代码
function getSslPage($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}


$user=getSslPage($url);
// $user=file_get_contents($url);
// echo $user;exit;
$access_token=explode('access_token":"',$user)[1];
$access_token=explode('"',$user)[0];//access_token
$openid=explode('"openid":"',$user)[1];
$openid=explode('"',$openid)[0];//openid
$refresh_token=explode('refresh_token":"',$user)[1];
$refresh_token=explode('"',$refresh_token)[0];//refresh_token

$aa=json($user);
echo $aa->access_token;


// $user_info_url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
$use_url='https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$refresh_token;
$user_cont=getSslPage($use_url);
// echo $user_cont;exit;
$access_token=explode('access_token":"',$user_cont)[1];
$access_token=explode('"',$access_token)[0];//access_token
$openid=explode('"openid":"',$user_cont)[1];
$openid=explode('"',$openid)[0];//openid
$refresh_token=explode('refresh_token":"',$user_cont)[1];
$refresh_token=explode('"',$refresh_token)[0];//refresh_token

$user_info='https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN;';
$text=getSslPage($user_info);
$arr=json_decode($text,true);
echo '网名:'.$arr['nickname'].'</br>';
echo '头像:<img src='.$arr['headimgurl'].'></br>';
然后微信访问这个weixin.php文件就能看到你的头像和网名了

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页