> php教程 > php手册 > 微信开发小结-PHP

微信开发小结-PHP

WBOY
풀어 주다: 2016-07-09 09:10:24
원래의
1259명이 탐색했습니다.

功能点:

1、  网页授权获得微信用户信息

用户在微信客户端中访问第三方网页,公众号可以通过微信网页授权机制,来获取用户基本信息,进而实现业务逻辑。

注意点:Scope为snsapi_base 只能获得用户的openid,为snsapi_userinfo(会跳出一个授权页面)时可以获得openid,nicknam,sex,province等详见接口文档。

应用场景:第一次需要授权,后面无需授权。

          解决方案,第一次为snsapi_userinfo,保存信息,非第一次去检查这个openid是否已存在 如果不存在 跳转至snsapi_userinfo的页面授权,否则就直接从读取信息。

2、  分享接口

该接口较简单,引入js,按照官方文档说明就行

3、  微信支付

开通微信支付功能,下载官方的Demo,lib/WxPay.Config.php 配置各个信息,证书位置建议使用绝对位置,防止被下载。

<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>);
로그인 후 복사
<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>
로그인 후 복사

 回调

<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>);
로그인 후 복사

 

4、  后台自动发送红包接口

构建发送信息,红包金额1-200之间  0~8点不触发!

<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>
로그인 후 복사

 

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿