保举:《PHP视频教程》
php开发微信大众号文章付费浏览性能!
如上图,一看就懂,就是一片大众号文章,点出来显示题目,作者,工夫,大众号称号以及局部内容,要想浏览一切内容,那就领取0.01元就能够浏览一切的内容,这就是付费浏览!!!当然金额能够自界说....
其实这个开发原理很简略,无非就是正在文章页面加一个微信领取的按钮,点击领取后把暗藏的局部给显示进去或许间接跳转到全文地点。
当然了,这个文章界面也是模拟民间来写的,你能够查看民间的款式,而后模拟就能够了,这个没啥难度。次要是加个按钮正在这个页面,上面是这个页面的代码:
readpay.php
<?php header('Content-type:text/html; Charset=utf-8'); $mchid = '微信领取商户号'; //微信领取商户号 $appid = '微信领取请求对应的大众号的APPID'; //微信领取请求对应的大众号的APPID $appKey = '微信领取请求对应的大众号的APPSECRET'; //微信领取请求对应的大众号的APPSECRET $apiKey = 'API密钥'; //帐户设置-平安设置-API平安-API密钥-设置API密钥 //①、猎取用户openid $wxPay = new WxpayService($mchid,$appid,$appKey,$apiKey); $openId = $wxPay->GetOpenid(); //猎取openid if(!$openId) exit('猎取openid失败'); //②、对立下单 $outTradeNo = uniqid(); //你本人的商品定单号 $payAmount = 0.01; //付款金额,单元:元 $orderName = 'test'; //定单题目 $notifyUrl = 'nofity.php'; //付款胜利后的回调地点,普通放正在本页面的同级目次便可 $payTime = time(); //付款工夫 $jsApiParameters = $wxPay->createJsBizPackage($openId,$payAmount,$outTradeNo,$orderName,$notifyUrl,$payTime); $jsApiParameters = json_encode($jsApiParameters); ?> <!--上面就是木方民间微信文章页面--> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"/> <title>新版微信支持双开,王利芬庆贺“茅侃侃逝世”文浏览10w+被批人血馒头</title> <script type="text/javascript"> //挪用微信JS api 领取 function jsApiCall() { WeixinJSBridge.invoke( 'getBrandWCPayRequest', <?php echo $jsApiParameters; ?>, function(res){ WeixinJSBridge.log(res.err_msg); //alert(res.err_code+res.err_desc+res.err_msg); if(res.err_msg == "get_brand_wcpay_request:ok"){ //领取胜利跳转页面 window.location.href="这里填写领取胜利后要跳转的查看全文文章地点"; }else{ //领取失败/或勾销领取跳转页面,本人自界说开发页面 window.location.href="false.html"; } } ); } function callpay() { if (typeof WeixinJSBridge == "undefined"){ if( document.addEventListener ){ document.addEventListener('WeixinJSBridgeReady', jsApiCall, false); }else if (document.attachEvent){ document.attachEvent('WeixinJSBridgeReady', jsApiCall); document.attachEvent('onWeixinJSBridgeReady', jsApiCall); } }else{ jsApiCall(); } } </script> <!--上面就是木方民间微信文章页面的CSS--> <style> #title{ margin-bottom:10px; line-height:1.4; font-weight:400; font-size:24px; width: 97%; margin:0 auto; } #meta_content .time{ color: #8C8C8C; font-family: "SimHei"; } .author{ color: #8C8C8C; font-family: "SimHei"; } .name{ color: #607fa6; } #con{ width:95%; font-size: 15px; margin:15px auto 25px; color: #595757; } </style> </head> <body> <br/> <!--文章题目--> <h2 id="title">新版微信支持双开,王利芬庆贺“茅侃侃逝世”文浏览10w+被批人血馒头</h2> <div id="meta_content"> <!--公布工夫、作者、大众号昵称--> <span class="time">2018-01-27</span> <span class="author">tanking</span> <span class="name">里客云</span> </div> <!--显示的局部内容,未领取前显示的内容--> <div id="con"> 这一周,大众号谈论最多的有“周冲的影像声色”因洗稿被撕、80后守业者茅侃侃逝世、刘亦菲离别......简直每一件事都能正在冤家圈刷到相干报导。别的,视频年夜号“一条”又拿到了新一轮融资,你最关怀哪一件事呢? </div> <!--领取按钮--> <div align="center"> <button style="width:180px; height:35px;background: none; border-radius: 5px;border:1px #1AAD19 solid; cursor: pointer; color:#1AAD19; font-size:15px;-webkit-tap-highlight-color:rgba(255,0,0,0);" type="button" οnclick="callpay()" >领取0.01浏览全文</button> </div> </body> </html> <!--上面是微信领取逻辑了--> <?php header("Content-Type:text/html; charset=utf-8"); class WxpayService { protected $mchid; protected $appid; protected $appKey; protected $apiKey; public $data = null; public function __construct($mchid, $appid, $appKey,$key) { $this->mchid = $mchid; //https://pay.weixin.qq.com 产物中心-开发设置装备摆设-商户号 $this->appid = $appid; //微信领取请求对应的大众号的APPID $this->appKey = $appKey; //微信领取请求对应的大众号的APP Key $this->apiKey = $key; //https://pay.weixin.qq.com 帐户设置-平安设置-API平安-API密钥-设置API密钥 } /** * 经过跳转猎取用户的openid,跳转流程以下: * 一、设置本人需求调回的url及其其余参数,跳转到微服气务器https://open.weixin.qq.com/connect/oauth2/authorize * 二、微服气务解决实现之后会跳转回用户redirect_uri地点,此时会带上一些参数,如:code * @return 用户的openid */ public function GetOpenid() { //经过code取得openid if (!isset($_GET['code'])){ //触发微信前往code码 $scheme = $_SERVER['HTTPS']=='on' ? 'https://' : 'http://'; $baseUrl = urlencode($scheme.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].$_SERVER['QUERY_STRING']); $url = $this->__CreateOauthUrlForCode($baseUrl); Header("Location: $url"); exit(); } else { //猎取code码,以猎取openid $code = $_GET['code']; $openid = $this->getOpenidFromMp($code); return $openid; } } /** * 经过code从工作平台猎取openid机械access_token * @param string $code 微信跳转回来带上的code * @return openid */ public function GetOpenidFromMp($code) { $url = $this->__CreateOauthUrlForOpenid($code); $res = self::curlGet($url); //掏出openid $data = json_decode($res,true); $this->data = $data; $openid = $data['openid']; return $openid; } /** * 结构猎取open以及access_toke的url地点 * @param string $code,微信跳转带回的code * @return 申请的url */ private function __CreateOauthUrlForOpenid($code) { $urlObj["appid"] = $this->appid; $urlObj["secret"] = $this->appKey; $urlObj["code"] = $code; $urlObj["grant_type"] = "authorization_code"; $bizString = $this->ToUrlParams($urlObj); return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString; } /** * 结构猎取code的url衔接 * @param string $redirectUrl 微服气务器回跳的url,需求url编码 * @return 前往结构好的url */ private function __CreateOauthUrlForCode($redirectUrl) { $urlObj["appid"] = $this->appid; $urlObj["redirect_uri"] = "$redirectUrl"; $urlObj["response_type"] = "code"; $urlObj["scope"] = "snsapi_base"; $urlObj["state"] = "STATE"."#wechat_redirect"; $bizString = $this->ToUrlParams($urlObj); return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString; } /** * 拼接署名字符串 * @param array $urlObj * @return 前往曾经拼接好的字符串 */ private function ToUrlParams($urlObj) { $buff = ""; foreach ($urlObj as $k => $v) { if($k != "sign") $buff .= $k . "=" . $v . "&"; } $buff = trim($buff, "&"); return $buff; } /** * 对立下单 * @param string $openid 挪用【网页受权猎取用户信息】接口猎取到用户正在该大众号下的Openid * @param float $totalFee 收款总用度 单元元 * @param string $outTradeNo 惟一的定单号 * @param string $orderName 定单称号 * @param string $notifyUrl 领取后果告诉url 没有要有问号 * @param string $timestamp 领取工夫 * @return string */ public function createJsBizPackage($openid, $totalFee, $outTradeNo, $orderName, $notifyUrl, $timestamp) { $config = array( 'mch_id' => $this->mchid, 'appid' => $this->appid, 'key' => $this->apiKey, ); $orderName = iconv('GBK','UTF-8',$orderName); $unified = array( 'appid' => $config['appid'], 'attach' => 'pay', //商家数据包,原样前往,假如填写中文,请留意转换为utf-8 'body' => $orderName, 'mch_id' => $config['mch_id'], 'nonce_str' => self::createNonceStr(), 'notify_url' => $notifyUrl, 'openid' => $openid, //rade_type=JSAPI,此参数必传 'out_trade_no' => $outTradeNo, 'spbill_create_ip' => '127.0.0.1', 'total_fee' => intval($totalFee * 100), //单元 转为分 'trade_type' => 'JSAPI', ); $unified['sign'] = self::getSign($unified, $config['key']); $responseXml = self::curlPost('https://api.mch.weixin.qq.com/pay/unifiedorder', self::arrayToXml($unified)); $unifiedOrder = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA); if ($unifiedOrder === false) { die('parse xml error'); } if ($unifiedOrder->return_code != 'SUCCESS') { die($unifiedOrder->return_msg); } if ($unifiedOrder->result_code != 'SUCCESS') { die($unifiedOrder->err_code); } $arr = array( "appId" => $config['appid'], "timeStamp" => "$timestamp", //这里是字符串的工夫戳,没有是int,以是需加引号 "nonceStr" => self::createNonceStr(), "package" => "prepay_id=" . $unifiedOrder->prepay_id, "signType" => 'MD5', ); $arr['paySign'] = self::getSign($arr, $config['key']); return $arr; } public static function curlGet($url = '', $options = array()) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 30); if (!empty($options)) { curl_setopt_array($ch, $options); } //https申请 没有验证证书以及host curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $data = curl_exec($ch); curl_close($ch); return $data; } public static function curlPost($url = '', $postData = '', $options = array()) { if (is_array($postData)) { $postData = http_build_query($postData); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL容许执行的最长秒数 if (!empty($options)) { curl_setopt_array($ch, $options); } //https申请 没有验证证书以及host curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $data = curl_exec($ch); curl_close($ch); return $data; } public static function createNonceStr($length = 16) { $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; $str = ''; for ($i = 0; $i < $length; $i++) { $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } return $str; } public static function arrayToXml($arr) { $xml = "<xml>"; foreach ($arr as $key => $val) { if (is_numeric($val)) { $xml .= "<" . $key . ">" . $val . "</" . $key . ">"; } else $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">"; } $xml .= "</xml>"; return $xml; } public static function getSign($params, $key) { ksort($params, SORT_STRING); $unSignParaString = self::formatQueryParaMap($params, false); $signStr = strtoupper(md5($unSignParaString . "&key=" . $key)); return $signStr; } protected static function formatQueryParaMap($paraMap, $urlEncode = false) { $buff = ""; ksort($paraMap); foreach ($paraMap as $k => $v) { if (null != $v && "null" != $v) { if ($urlEncode) { $v = urlencode($v); } $buff .= $k . "=" . $v . "&"; } } $reqPar = ''; if (strlen($buff) > 0) { $reqPar = substr($buff, 0, strlen($buff) - 1); } return $reqPar; } } ?>
nofity.php
<?php /** * 原生领取(扫码领取)及大众号领取的异步回调告诉 * 阐明:需求正在native.php或许jsapi.php中的填写回调地点。例如:http://www.xxx.com/wx/notify.php * 付款胜利后,微服气务器会将付款后果告诉到该页面 */ header('Content-type:text/html; Charset=utf-8'); $mchid = '微信领取商户号'; $appid = '大众号APPID'; $apiKey = 'API密钥'; $wxPay = new WxpayService($mchid,$appid,$apiKey); $result = $wxPay->notify(); if($result){ //实现你的逻辑 //例如衔接数据库,猎取付款金额$result['cash_fee'],猎取定单号$result['out_trade_no'],修正数据库中的定单状态等; }else{ echo 'pay error'; } class WxpayService { protected $mchid; protected $appid; protected $apiKey; public function __construct($mchid, $appid, $key) { $this->mchid = $mchid; $this->appid = $appid; $this->apiKey = $key; } public function notify() { $config = array( 'mch_id' => $this->mchid, 'appid' => $this->appid, 'key' => $this->apiKey, ); $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); if ($postObj === false) { die('parse xml error'); } if ($postObj->return_code != 'SUCCESS') { die($postObj->return_msg); } if ($postObj->result_code != 'SUCCESS') { die($postObj->err_code); } $arr = (array)$postObj; unset($arr['sign']); if (self::getSign($arr, $config['key']) == $postObj->sign) { echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>'; return $arr; } } /** * 猎取署名 */ public static function getSign($params, $key) { ksort($params, SORT_STRING); $unSignParaString = self::formatQueryParaMap($params, false); $signStr = strtoupper(md5($unSignParaString . "&key=" . $key)); return $signStr; } protected static function formatQueryParaMap($paraMap, $urlEncode = false) { $buff = ""; ksort($paraMap); foreach ($paraMap as $k => $v) { if (null != $v && "null" != $v) { if ($urlEncode) { $v = urlencode($v); } $buff .= $k . "=" . $v . "&"; } } $reqPar = ''; if (strlen($buff) > 0) { $reqPar = substr($buff, 0, strlen($buff) - 1); } return $reqPar; } }
简略阐明:
一、新建readpay.php,拷贝下面的代码,修正商户号、appid、appsecret、密钥便可,另有上面的回调地点的门路(nofity.php),至于领取胜利以及领取失败跳转的页面按本人的需要修正便可。
领取金额、文章题目】工夫、作者、公账号称号等依据本人的需要修正。
要留意,定单题目不克不及为中文,不然会提醒body参数谬误甚么的。
二、新建nofity.php
把下面的代码拷贝下来,修正商户号、appid、密钥便可。
上传到领取受权目次,而后拜访readpay.php页面测试。
三、新建false.html,这个是领取失败的页面,至于领取失败要显示甚么或许输入甚么,这个依据你的营业来自界说开发。
以上就是教你用PHP开发微信大众号文章付费浏览性能的具体内容,更多请存眷资源魔其它相干文章!
标签: php php开发教程 php开发资料 php开发自学
抱歉,评论功能暂时关闭!