Home > php教程 > php手册 > body text

WeChat development summary-PHP

WBOY
Release: 2016-07-09 09:10:24
Original
1248 people have browsed it

Function points:

1. Web page authorization to obtain WeChat user information

When users access third-party web pages in the WeChat client, public accounts can obtain basic user information through the WeChat web page authorization mechanism, and then implement business logic.

Note: When Scope is snsapi_base, you can only get the user's openid. When it is snsapi_userinfo (an authorization page will pop up), you can get openid, nicknam, sex, province, etc. For details, please refer to the interface document.

Application scenario: Authorization is required for the first time, but no authorization is required later.

The solution is to save the information for snsapi_userinfo for the first time. If it is not the first time, check whether the openid already exists. If it does not exist, jump to the snsapi_userinfo page for authorization. Otherwise, read the information directly.

2. Sharing interface

The interface is relatively simple, just introduce js and follow the official documentation

3. WeChat payment

Activate the WeChat payment function, download the official Demo, lib/WxPay.Config.php and configure various information. It is recommended to use an absolute position for the certificate location to prevent it from being downloaded.

<span style="color: #008000;">//</span><span style="color: #008000;">后台生成</span>
<span style="color: #800080;">$tools</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> JsApiPay();
</span><span style="color: #800080;">$openId</span> = <span style="color: #800080;">$tools</span>->GetOpenid("?id=".<span style="color: #800080;">$id</span>."&Id=".<span style="color: #800080;">$_REQUEST</span>["Id"]."&title=".<span style="color: #008080;">urlencode</span>(<span style="color: #800080;">$title</span><span style="color: #000000;">));
</span><span style="color: #008000;">//</span><span style="color: #008000;">GetOpenid 官方是无参调用,这里因为获得openid 是跳转根据code来实现原来 request的参数会丢失,所以在跳转的时候把需要的参数加上,需修改下这个方法,注意这里的title 中文的话需转一下 </span>
<span style="color: #800080;">$title</span>=<span style="color: #008080;">iconv</span>('gbk', 'utf-8',<span style="color: #800080;">$title</span><span style="color: #000000;">);
</span><span style="color: #800080;">$trade_no</span>=WxPayConfig::MCHID.<span style="color: #008080;">date</span>("YmdHis"<span style="color: #000000;">);
</span><span style="color: #800080;">$trade_no</span>=<span style="color: #800080;">$trade_no</span>.<span style="color: #008080;">rand</span>(100,999<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">统一下单</span>
<span style="color: #800080;">$input</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> WxPayUnifiedOrder();
</span><span style="color: #800080;">$input</span>->SetBody(<span style="color: #800080;">$title</span><span style="color: #000000;">);
</span><span style="color: #800080;">$input</span>->SetAttach(<span style="color: #800080;">$title</span><span style="color: #000000;">);
</span><span style="color: #800080;">$input</span>->SetOut_trade_no(<span style="color: #800080;">$trade_no</span><span style="color: #000000;">);
</span><span style="color: #800080;">$input</span>->SetTotal_fee(1);<span style="color: #008000;">//</span><span style="color: #008000;">单位分</span>
<span style="color: #800080;">$input</span>->SetTime_start(<span style="color: #008080;">date</span>("YmdHis"<span style="color: #000000;">));
</span><span style="color: #800080;">$input</span>->SetTime_expire(<span style="color: #008080;">date</span>("YmdHis", <span style="color: #008080;">time</span>() + 600<span style="color: #000000;">));
</span><span style="color: #800080;">$input</span>->SetGoods_tag(""<span style="color: #000000;">);
</span><span style="color: #800080;">$input</span>>SetNotify_url("http://www.xxxxxx.com/notify.php"<span style="color: #000000;">);
</span><span style="color: #800080;">$input</span>->SetTrade_type("JSAPI"<span style="color: #000000;">);
</span><span style="color: #800080;">$input</span>->SetOpenid(<span style="color: #800080;">$openId</span><span style="color: #000000;">);
</span><span style="color: #800080;">$order</span> = WxPayApi::unifiedOrder(<span style="color: #800080;">$input</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">参数列表 供JS调用</span>
<span style="color: #800080;">$jsApiParameters</span> = <span style="color: #800080;">$tools</span>->GetJsApiParameters(<span style="color: #800080;">$order</span><span style="color: #000000;">);
</span><span style="color: #800080;">$smarty</span>->assign("jsApiParameters", <span style="color: #800080;">$jsApiParameters</span>);
Copy after login
<span style="color: #008000;">//</span><span style="color: #008000;">前台调用微信JS api 支付</span>
        <span style="color: #0000ff;">function</span><span style="color: #000000;"> jsApiCall()
        {
        
            WeixinJSBridge.invoke(
                </span>'getBrandWCPayRequest'<span style="color: #000000;">,
                </span><span style="color: #000000;">,
                </span><span style="color: #0000ff;">function</span><span style="color: #000000;">(res){
                    WeixinJSBridge.log(res.err_msg);
                    </span><span style="color: #008000;">//</span><span style="color: #008000;">alert(res.err_code+res.err_desc+res.err_msg);</span>
<span style="color: #000000;">                    
                    
                }
            );
        }

        </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> callpay()
        {
            

         </span><span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">typeof</span> WeixinJSBridge == "undefined"<span style="color: #000000;">){
                </span><span style="color: #0000ff;">if</span><span style="color: #000000;">( document.addEventListener ){
                    document.addEventListener(</span>'WeixinJSBridgeReady', jsApiCall, <span style="color: #0000ff;">false</span><span style="color: #000000;">);
                }</span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span><span style="color: #000000;"> (document.attachEvent){
                    document.attachEvent(</span>'WeixinJSBridgeReady'<span style="color: #000000;">, jsApiCall); 
                    document.attachEvent(</span>'onWeixinJSBridgeReady'<span style="color: #000000;">, jsApiCall);
                }
            }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
                jsApiCall();
            }
            
        }</span>
Copy after login

Callback

<span style="color: #000000;">php

</span><span style="color: #0000ff;">require_once</span> "lib/WxPay.Api.php"<span style="color: #000000;">;
</span><span style="color: #0000ff;">require_once</span> 'lib/WxPay.Notify.php'<span style="color: #000000;">;
</span><span style="color: #0000ff;">require_once</span> 'unit/log.php'<span style="color: #000000;">;

</span><span style="color: #008000;">//</span><span style="color: #008000;">初始化日志</span>
<span style="color: #800080;">$logHandler</span>= <span style="color: #0000ff;">new</span> CLogFileHandler("./logs/".<span style="color: #008080;">date</span>('Y-m-d').'.log'<span style="color: #000000;">);
</span><span style="color: #800080;">$log</span> = <span style="color: #008080;">Log</span>::Init(<span style="color: #800080;">$logHandler</span>, 15<span style="color: #000000;">);

</span><span style="color: #0000ff;">class</span> PayNotifyCallBack <span style="color: #0000ff;">extends</span><span style="color: #000000;"> WxPayNotify
{
    </span><span style="color: #008000;">//</span><span style="color: #008000;">查询订单</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> Queryorder(<span style="color: #800080;">$transaction_id</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$input</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> WxPayOrderQuery();
        </span><span style="color: #800080;">$input</span>->SetTransaction_id(<span style="color: #800080;">$transaction_id</span><span style="color: #000000;">);
        </span><span style="color: #800080;">$result</span> = WxPayApi::orderQuery(<span style="color: #800080;">$input</span><span style="color: #000000;">);
        </span><span style="color: #008080;">Log</span>::DEBUG("query:" . json_encode(<span style="color: #800080;">$result</span><span style="color: #000000;">));
        </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">array_key_exists</span>("return_code", <span style="color: #800080;">$result</span><span style="color: #000000;">)
            </span>&& <span style="color: #008080;">array_key_exists</span>("result_code", <span style="color: #800080;">$result</span><span style="color: #000000;">)
            </span>&& <span style="color: #800080;">$result</span>["return_code"] == "SUCCESS"
            && <span style="color: #800080;">$result</span>["result_code"] == "SUCCESS"<span style="color: #000000;">)
        {
            </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;
        }
        </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
    }
    
    </span><span style="color: #008000;">//</span><span style="color: #008000;">重写回调处理函数</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> NotifyProcess(<span style="color: #800080;">$data</span>, &<span style="color: #800080;">$msg</span><span style="color: #000000;">)
    {
    
        </span><span style="color: #008080;">Log</span>::DEBUG("call back:" . json_encode(<span style="color: #800080;">$data</span><span style="color: #000000;">));
        </span><span style="color: #800080;">$notfiyOutput</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();
        
        </span><span style="color: #0000ff;">if</span>(!<span style="color: #008080;">array_key_exists</span>("transaction_id", <span style="color: #800080;">$data</span><span style="color: #000000;">)){
            </span><span style="color: #800080;">$msg</span> = "输入参数不正确"<span style="color: #000000;">;
            </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
        }
        </span><span style="color: #008000;">//</span><span style="color: #008000;">查询订单,判断订单真实性</span>
        <span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$this</span>->Queryorder(<span style="color: #800080;">$data</span>["transaction_id"<span style="color: #000000;">])){
            </span><span style="color: #800080;">$msg</span> = "订单查询失败"<span style="color: #000000;">;
            </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
        }
        </span><span style="color: #008000;">//</span><span style="color: #008000;">根据 $data["out_trade_no"]  订单号 更新订单状态
        //执行更新</span>
    
        <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;
    }
}

</span><span style="color: #008080;">Log</span>::DEBUG("begin notify"<span style="color: #000000;">);
</span><span style="color: #800080;">$notify</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> PayNotifyCallBack();
</span><span style="color: #800080;">$notify</span>->Handle(<span style="color: #0000ff;">false</span>);
Copy after login

4. Automatically send red envelope interface in the background

Construct and send information, the red envelope amount is between 1-200, and it will not trigger at 0~8 o'clock!

<span style="color: #0000ff;">function</span> curl_post_ssl(<span style="color: #800080;">$url</span>, <span style="color: #800080;">$vars</span>, <span style="color: #800080;">$second</span>=30,<span style="color: #800080;">$aHeader</span>=<span style="color: #0000ff;">array</span><span style="color: #000000;">())
{
    </span><span style="color: #800080;">$ch</span> =<span style="color: #000000;"> curl_init();
    
    curl_setopt(</span><span style="color: #800080;">$ch</span>,CURLOPT_TIMEOUT,<span style="color: #800080;">$second</span><span style="color: #000000;">);
    curl_setopt(</span><span style="color: #800080;">$ch</span>,CURLOPT_RETURNTRANSFER, 1<span style="color: #000000;">);
        
    </span><span style="color: #008000;">//</span><span style="color: #008000;">curl_setopt($ch,CURLOPT_PROXY, '10.206.30.98');
    //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);</span>
    curl_setopt(<span style="color: #800080;">$ch</span>,CURLOPT_URL,<span style="color: #800080;">$url</span><span style="color: #000000;">);
    curl_setopt(</span><span style="color: #800080;">$ch</span>,CURLOPT_SSL_VERIFYPEER,<span style="color: #0000ff;">false</span><span style="color: #000000;">);
    curl_setopt(</span><span style="color: #800080;">$ch</span>,CURLOPT_SSL_VERIFYHOST,<span style="color: #0000ff;">false</span><span style="color: #000000;">);
    
    
    </span><span style="color: #008000;">//</span><span style="color: #008000;">curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
    //curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/cert.pem');</span>
<span style="color: #000000;">    
        curl_setopt(</span><span style="color: #800080;">$ch</span>,CURLOPT_SSLCERTTYPE,'PEM'<span style="color: #000000;">);
    curl_setopt(</span><span style="color: #800080;">$ch</span>,CURLOPT_SSLCERT,<span style="color: #008080;">getcwd</span>().'/cert/rootca.pem'<span style="color: #000000;">);
    curl_setopt(</span><span style="color: #800080;">$ch</span>,CURLOPT_SSLCERTTYPE,'PEM'<span style="color: #000000;">);
    curl_setopt(</span><span style="color: #800080;">$ch</span>,CURLOPT_SSLCERT,<span style="color: #008080;">getcwd</span>().'/cert/apiclient_cert.pem'<span style="color: #000000;">);

    curl_setopt(</span><span style="color: #800080;">$ch</span>,CURLOPT_SSLKEYTYPE,'PEM'<span style="color: #000000;">);
    curl_setopt(</span><span style="color: #800080;">$ch</span>,CURLOPT_SSLKEY,<span style="color: #008080;">getcwd</span>().'/cert/apiclient_key.pem'<span style="color: #000000;">);
    
    
    </span><span style="color: #008000;">//</span><span style="color: #008000;">curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
    //curl_setopt($ch,CURLOPT_SSLKEY,getcwd().'/private.pem');
    
    
    //curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/all.pem');</span>
 
    <span style="color: #0000ff;">if</span>( <span style="color: #008080;">count</span>(<span style="color: #800080;">$aHeader</span>) >= 1<span style="color: #000000;"> ){
        curl_setopt(</span><span style="color: #800080;">$ch</span>, CURLOPT_HTTPHEADER, <span style="color: #800080;">$aHeader</span><span style="color: #000000;">);
    }
 
    curl_setopt(</span><span style="color: #800080;">$ch</span>,CURLOPT_POST, 1<span style="color: #000000;">);
    curl_setopt(</span><span style="color: #800080;">$ch</span>,CURLOPT_POSTFIELDS,<span style="color: #800080;">$vars</span><span style="color: #000000;">);
    </span><span style="color: #800080;">$data</span> = curl_exec(<span style="color: #800080;">$ch</span><span style="color: #000000;">);
    </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$data</span><span style="color: #000000;">){
        curl_close(</span><span style="color: #800080;">$ch</span><span style="color: #000000;">);
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$data</span><span style="color: #000000;">;
    }
    </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { 
        </span><span style="color: #800080;">$error</span> = curl_errno(<span style="color: #800080;">$ch</span><span style="color: #000000;">);
        </span><span style="color: #0000ff;">echo</span> "call faild, errorCode:<span style="color: #800080;">$error</span>\n"<span style="color: #000000;">; 
        curl_close(</span><span style="color: #800080;">$ch</span><span style="color: #000000;">);
        </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
    }
}






</span><span style="color: #008000;">//</span><span style="color: #008000;">$money</span>
<span style="color: #0000ff;">function</span> sendMoney(<span style="color: #800080;">$money</span>,<span style="color: #800080;">$openid</span>,<span style="color: #800080;">$uid</span><span style="color: #000000;">)
{

</span><span style="color: #800080;">$money</span>=<span style="color: #800080;">$money</span><span style="color: #000000;">;
</span><span style="color: #800080;">$mch_id</span>="123456777";<span style="color: #008000;">//</span><span style="color: #008000;">商户ID</span>
<span style="color: #800080;">$nonce_str</span>=<span style="color: #008080;">date</span>("YmdHis").<span style="color: #008080;">rand</span>(9999,189900<span style="color: #000000;">);
</span><span style="color: #800080;">$mch_billno</span>=<span style="color: #008080;">date</span>("YmdHis").<span style="color: #008080;">rand</span>(1000,9999<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">date("Ymd").rand(10000000,9999999999);</span>
<span style="color: #800080;">$wxappid</span>="sx4324rererer3434";<span style="color: #008000;">//</span><span style="color: #008000;">appid</span>
<span style="color: #800080;">$nick_name</span>="test"<span style="color: #000000;">;
</span><span style="color: #800080;">$send_name</span>="test"<span style="color: #000000;">;
</span><span style="color: #800080;">$re_openid</span>=<span style="color: #800080;">$openid</span><span style="color: #000000;">;
</span><span style="color: #800080;">$total_amount</span>=<span style="color: #800080;">$money</span>;<span style="color: #008000;">//</span><span style="color: #008000;">$total_amount、$min_value、$max_value一致</span>
<span style="color: #800080;">$min_value</span>=<span style="color: #800080;">$money</span><span style="color: #000000;">;
</span><span style="color: #800080;">$max_value</span>=<span style="color: #800080;">$money</span><span style="color: #000000;">;
</span><span style="color: #800080;">$total_num</span>="1";<span style="color: #008000;">//</span><span style="color: #008000;">红包个数</span>
<span style="color: #800080;">$wishing</span>="test"<span style="color: #000000;">;
</span><span style="color: #800080;">$client_ip</span>="xxx.xx.xxx.xx";<span style="color: #008000;">//</span><span style="color: #008000;">服务器IP地址</span>
<span style="color: #800080;">$act_name</span>="佣金"<span style="color: #000000;">;
</span><span style="color: #800080;">$remark</span>="恭喜您获得红包,再接再励哦!"<span style="color: #000000;">;
</span><span style="color: #800080;">$stringA</span>="act_name=<span style="color: #800080;">$act_name</span>&client_ip=<span style="color: #800080;">$client_ip</span>&max_value=<span style="color: #800080;">$max_value</span>&mch_billno=<span style="color: #800080;">$mch_billno</span>&mch_id=<span style="color: #800080;">$mch_id</span>&min_value=<span style="color: #800080;">$min_value</span>&nick_name=<span style="color: #800080;">$nick_name</span>&nonce_str=<span style="color: #800080;">$nonce_str</span>&re_openid=<span style="color: #800080;">$re_openid</span>&remark=<span style="color: #800080;">$remark</span>&send_name=<span style="color: #800080;">$send_name</span>&total_amount=<span style="color: #800080;">$total_amount</span>&total_num=<span style="color: #800080;">$total_num</span>&wishing=<span style="color: #800080;">$wishing</span>&wxappid=<span style="color: #800080;">$wxappid</span>"<span style="color: #000000;">;
</span><span style="color: #008000;">//</span><span style="color: #008000;">注意这个stringA 花在这上面不少时间,没仔细看官方的信息,一直报签名错误,这个参数列表要按照参数名ASCII码从小到大排序,参数跟下面构建的xml数据一样
//建议从这里生成在放到这里特别是多个参数名称类似http://mch.weixin.qq.com/wiki/tools/signverify/</span>

<span style="color: #800080;">$stringSignTemp</span>="<span style="color: #800080;">$stringA</span>&key=e10adcaba59abbe56e057120fa3esxCX"<span style="color: #000000;">;
</span><span style="color: #800080;">$sign</span>=<span style="color: #008080;">strtoupper</span>(<span style="color: #008080;">md5</span>(<span style="color: #800080;">$stringSignTemp</span><span style="color: #000000;">));
</span><span style="color: #800080;">$data</span>="<span style="color: #000000;"><xml>
            <nonce_str></nonce_str></xml></span><span style="color: #800080;">$nonce_str</span><span style="color: #000000;">
            <sign></sign></span><span style="color: #800080;">$sign</span><span style="color: #000000;">
            <mch_billno></mch_billno></span><span style="color: #800080;">$mch_billno</span><span style="color: #000000;">
            <mch_id></mch_id></span><span style="color: #800080;">$mch_id</span><span style="color: #000000;">
            <wxappid></wxappid></span><span style="color: #800080;">$wxappid</span><span style="color: #000000;">
            <nick_name></nick_name></span><span style="color: #800080;">$nick_name</span><span style="color: #000000;">
            <send_name></send_name></span><span style="color: #800080;">$send_name</span><span style="color: #000000;">
            <re_openid></re_openid></span><span style="color: #800080;">$re_openid</span><span style="color: #000000;">
            <total_amount></total_amount></span><span style="color: #800080;">$total_amount</span><span style="color: #000000;">
            <min_value></min_value></span><span style="color: #800080;">$min_value</span><span style="color: #000000;">
            <max_value></max_value></span><span style="color: #800080;">$max_value</span><span style="color: #000000;">
            <total_num></total_num></span><span style="color: #800080;">$total_num</span><span style="color: #000000;">
            <wishing></wishing></span><span style="color: #800080;">$wishing</span><span style="color: #000000;">
            <client_ip></client_ip></span><span style="color: #800080;">$client_ip</span><span style="color: #000000;">
            </span><span style="color: #800080;">$act_name</span><span style="color: #000000;">
            <remark></remark></span><span style="color: #800080;">$remark</span><span style="color: #000000;"> 
        </span>"<span style="color: #000000;">;
  </span><span style="color: #800080;">$data1</span> = curl_post_ssl('https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack',<span style="color: #800080;">$data</span><span style="color: #000000;">);
  </span><span style="color: #800080;">$xml</span>=<span style="color: #008080;">simplexml_load_string</span>(<span style="color: #800080;">$data1</span><span style="color: #000000;">);
  </span><span style="color: #0000ff;">if</span>( <span style="color: #800080;">$xml</span>!=""<span style="color: #000000;">)
  {
</span><span style="color: #800080;">$ss</span>=<span style="color: #800080;">$xml</span>-><span style="color: #000000;">children();
  </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$ss</span>=="SUCCESS"<span style="color: #000000;">)
  { 
</span><span style="color: #008000;">//</span><span style="color: #008000;">执行自定义代码</span>
  <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;
  }
  </span><span style="color: #0000ff;">else</span>
  <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
  }
  </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
}</span>
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template