🚀 Is building a website too difficult? Let me guide you step by step—Learn about the 「Naibabiji WordPress Website Building Coaching Service」 →

Blog Traffic to Official Account Auto-Generated Verification Code: WeChat Official Platform Verification Code View Plugin

Previously, Naiba shared3 Methods to Implement Blog to Official Account Traffic, which included using a plugin that replies with keywords to obtain a password. However, the verification code for that plugin was manually entered by the site admin. The one recommended today automatically generates and retrieves verification codes, enabling automatic verification code rotation for traffic generation.Plugin code is as follows:
captcha = $wx_captcha;
	}
	private function checkSignature() {
		$signature = $_GET["signature"];
		$timestamp = $_GET["timestamp"];
		$nonce = $_GET["nonce"];
		$token = WX_TOKEN;
		$tmpArr = array($token, $timestamp, $nonce);
		sort($tmpArr, SORT_STRING);
		$tmpStr = implode($tmpArr);
		$tmpStr = sha1($tmpStr);
		if ($tmpStr == $signature)
		            return true; else
		            return false;
	}
	protected function valid() {
		$echoStr = $_GET["echostr"];
		//valid signature , option
		if ($this->checkSignature()) {
			echo $echoStr;
			exit;
		} else {
			echo 'error signature';
		}
	}
	public function responseMsg() {
		//如果是验证请求,则执行签名验证并退出
		if (!empty($_GET["echostr"])) {
			$this->valid();
			//验证签名是否有效
			return;
			//返回退出
		}
		if ($_SERVER['REQUEST_METHOD'] != 'POST') {
			echo '';
			return;
		}
		//如果不是验证请求,则
		//首先,取得POST原始数据(XML格式)
		//$postData = $GLOBALS["HTTP_RAW_POST_DATA"];
		$postData = file_get_contents('php://input');
		if (empty($postData)) {
			echo '';
			return;
		}
		//如果没有POST数据,则退出
		if (!empty($postData)) {
			//解析POST数据(XML格式)
			$object = simplexml_load_string($postData, 'SimpleXMLElement', LIBXML_NOCDATA);
			$messgeType = trim($object->MsgType);//取得消息类型
			$this->fromUser = "" . $object->FromUserName;
			$this->toUser = "" . $object->ToUserName;
			$keyword = trim($object->Content);
			if( $messgeType == 'text' && $keyword == '验证码') {
				$response_content = '您的验证码为:【'.$this->captcha.'】,验证码有效期为2分钟,请抓紧使用,过期需重新申请';
				$xmlTemplate = "
                    
                    
                    %s
                    
                    
                    %d
                    ";
				$xmlText = sprintf($xmlTemplate, $this->fromUser, $this->toUser, time(), $response_content, 0);
				echo $xmlText;
			}
		} else {
			echo "";
			exit;
		}
	}
}
//class end

//生成微信验证码
function wx_captcha(){
	date_default_timezone_set('Asia/Shanghai');
	$min = floor(date("i")/2);
	$day = date("d");
	$day = ltrim($day,0);
	$url = home_url();
	$captcha = sha1($min.$url.WX_TOKEN);
	$captcha = substr($captcha , $day , 6);
	return $captcha;

}

function wx_process() {
	global $object;
	$object = new Wechat_Captcha(wx_captcha());
	$object->responseMsg();
	return;
}
add_action('pre_get_posts', 'wx_process', 4);

/***  微信端结束 ***/

/***  WP端开始 ***/
//密码可见
function wx_captcha_view() {
	$action = $_POST['action'];
	$post_id = $_POST['id'];
	$pass = $_POST['pass'];
	$wxcaptcha = wx_captcha();
	if(!isset( $action )  ||  !isset( $post_id )  ||  !isset( $pass )   ) exit('400');
	if($pass == $wxcaptcha ) {
	$pass_content = get_post_meta($post_id, '_pass_content')[0];
	exit($pass_content);
	}else{
		exit('400');
	}
}
add_action('wp_ajax_nopriv_gdk_pass_view', 'wx_captcha_view');
add_action('wp_ajax_gdk_pass_view', 'wx_captcha_view');


// 部分内容输入密码可见
function gdk_secret_view($atts, $content = null) {
    $pid = get_the_ID();
    add_post_meta($pid, '_pass_content', $content, true) or update_post_meta($pid, '_pass_content', $content);
    if ( current_user_can( 'administrator' ) ) { return $content; }//admin show
        return '
		
'; } add_shortcode('wxcaptcha', 'gdk_secret_view'); //按钮 function wx_captcha_btn() { ?> There are only two places you need to configure:
// 此token必须和微信公众平台中的设置保持一致
define('WX_TOKEN', '');
define('WX_QR', '');//公众号二维码
One is wx_token, which can be obtained in the WeChat Official Account management backend, and the other is the QR code image address for your official account.Download Link Usage method:1. Download and install the plugin. 2. Modify the values of wx_token and wx_qr in the plugin. 3. When publishing a Post, use the shortcodes [wxcaptcha] and [\/wxcaptcha] to wrap the content you want to hide. Author's release address: https:\/\/gitcafe.net\/archives\/10584.html

🚀 Still feeling confused after reading the tutorial? Let me guide you step-by-step instead.

「Naibabiji WordPress Website Building Coaching」 — From selecting a domain and purchasing hosting to installing themes and publishing posts, I「ll guide you through every step, helping you avoid detours and reach your goals directly.

👉 Learn about Website Building Coaching Service
🔒

Comments are closed

The comment function for this article is closed. If you have any questions, please feel free to contact us through other channels.

×
二维码

Scan to Follow