PHP΢Ðźì°üAPI½Ó¿Ú

5年以前  |  阅读数:229 次  |  编程语言:PHP 

Ê×Ïȸø´o¼Ò¿´Ò»¿´Õa¸o±i¸ñ£º

¸u¾Ý΢П߼¶ºi°u½Ó¿Ú£¬¿ª¢PHP°ae±¾µÄAPI½Ó¿Ú£¬ÏÖÔÚ½øÐÐÖ÷Òª´uÂeÖÎo¡£

ºi°u½Ó¿Úµ÷ÓÃÇeÇo´uÂe£¬ËuÓÐÇeÇo²ÎÊýΪ±ØÌi²ÎÊýÓeÎĵµ¶ÔÓ¦£º


    class Wxapi {
     private $app_id = 'wxXXXXXXXXXXXX'; //¹«ÖÚÕ˺Åappid£¬Ê×ÏÈÉeÇeÓeÖ®ÅaÌ׵Ĺ«ÖÚÕ˺Å
     private $app_secret = 'XXXXXXXXXXXXXXXXXXXXXXXX';//¹«ÖÚºÅsecret£¬Óû§»ñÈ¡Óû§ÊÚȨtoken
     private $app_mchid = 'XXXXXXXX';//ÉÌ»§ºÅid
     function __construct(){
     //do sth here....
     }
     /**
      * ΢ÐÅÖ§¸¶
      * @param string $openid Óû§openid
      */
     public function pay($re_openid)
     {
      include_once('WxHongBaoHelper.php');
      $commonUtil = new CommonUtil();
      $wxHongBaoHelper = new WxHongBaoHelper();
      $wxHongBaoHelper->setParameter("nonce_str", $this->great_rand());//Ëae»u×Ö*u´®£¬Ø¢³¤ÓÚ 32 λ
      $wxHongBaoHelper->setParameter("mch_billno", $this->app_mchid.date('YmdHis').rand(1000, 9999));//¶(C)µ¥ºÅ
      $wxHongBaoHelper->setParameter("mch_id", $this->app_mchid);//ÉÌ»§ºÅ
      $wxHongBaoHelper->setParameter("wxappid", $this->app_id);
      $wxHongBaoHelper->setParameter("nick_name", 'ºi°u');//Ìa¹(C)*½Ãu³Æ
      $wxHongBaoHelper->setParameter("send_name", 'ºi°u');//ºi°u*¢ËÍÕßÃu³Æ
      $wxHongBaoHelper->setParameter("re_openid", $re_openid);//Ïa¶ÔÓÚÒ½Âo»¥Í¨µÄopenid
      $wxHongBaoHelper->setParameter("total_amount", 100);//¸¶¿i½ð¶i£¬µ¥Î»*Ö
      $wxHongBaoHelper->setParameter("min_value", 100);//×iСºi°u½ð¶i£¬µ¥Î»*Ö
      $wxHongBaoHelper->setParameter("max_value", 100);//×i´oºi°u½ð¶i£¬µ¥Î»*Ö
      $wxHongBaoHelper->setParameter("total_num", 1);//ºi°u…§*Å×ÜÈËÊý
      $wxHongBaoHelper->setParameter("wishing", '¸ÐлÄu²ÎÓeºi°uÅÉ*¢»i¶¯£¬×£ÄuÐÂÄe¿iÀÖ£¡');//ºi°u×£¸£²i
      $wxHongBaoHelper->setParameter("client_ip", '127.0.0.1');//µ÷ÓýӿڵĻuÆ÷ Ip µØÖ*
      $wxHongBaoHelper->setParameter("act_name", 'ºi°u»i¶¯');//»iÛ½Ãu³Æ
      $wxHongBaoHelper->setParameter("remark", '¿iÀ´ÇÀ£¡');//±¸×¢ÐÅÏ¢
      $postXml = $wxHongBaoHelper->create_hongbao_xml();
      $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';
      $responseXml = $wxHongBaoHelper->curl_post_ssl($url, $postXml);
      //ÓÃ×÷½a¹uµ÷ÊÔÊa³o
      //echo htmlentities($responseXml,ENT_COMPAT,'UTF-8');
     $responseObj = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);
     return $responseObj->return_code;
     }

*»ñÈ¡Ëae»u×Öu´®½¨£º**


    /**
     * Éu³ÉËae»uÊý
     */  
    public function great_rand(){
     $str = '1234567890abcdefghijklmnopqrstuvwxyz';
     for($i=0;$i<30;$i++){
      $j=rand(0,35);
      $t1 .= $str[$j];
     }
     return $t1; 
    }

*Ç(C)ÃuËa¨£º
**


    /**
    ÀýÈ磺
    appid£º wxd111665abv58f4f
    mch_id£º 10000100
    device_info£º 1000
    Body£º test
    nonce_str£º ibuaiVcKdpRxkhJA
    µÚÒ»²½£º¶Ô²ÎÊý°´ÕÕ key=value µÄ¸ñʽ£¬²¢°´ÕÕ²ÎÊýÃu ASCII ×ÖµaÐoÅÅÐoÈçÏ£º
    stringA="appid=wxd930ea5d5a258f4f&body;=test&device;_info=1000&mch;_i
    d=10000100&nonce;_str=ibuaiVcKdpRxkhJA";
    µÚ¶þ²½£ºÆ´½ÓÖ§¸¶ÃÜÔ¿£º
    stringSignTemp="stringA&key;=192006250b4c09247ec02edce69f6a2d"
    sign=MD5(stringSignTemp).toUpperCase()="9A0A8659F005D6984697E2CA0A
    9CF3B7"
    */
    protected function get_sign(){
     define('PARTNERKEY',"QSRXXXXXXXXXXXXXXXXXXXXX");
     try {
      if (null == PARTNERKEY || "" == PARTNERKEY ) {
       throw new SDKRuntimeException("ÃÜÔ¿²»ÄÜΪ¿Õ£¡" . "<br>");
      }
      if($this->check_sign_parameters() == false) { //¼i²eÉu³ÉÇ(C)Ãu²ÎÊý
       throw new SDKRuntimeException("Éu³ÉÇ(C)Ãu²ÎÊýȱʧ£¡" . "<br>");
      }
      $commonUtil = new CommonUtil();
      ksort($this->parameters);
      $unSignParaString = $commonUtil->formatQueryParaMap($this->parameters, false);
      $md5SignUtil = new MD5SignUtil();
      return $md5SignUtil->sign($unSignParaString,$commonUtil->trimString(PARTNERKEY));
     }catch (SDKRuntimeException $e)
     {
      die($e->errorMessage());
     }

    }

*CURLÇeÇoÒÔ¼°¢ËÍÖ¤Êe£º
**


    function curl_post_ssl($url, $vars, $second=30,$aHeader=array())
    {
     $ch = curl_init();
     //³¬Ê±Ê±¼a
     curl_setopt($ch,CURLOPT_TIMEOUT,$second);
     curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
     //ÕaÀiÉeÖôuÀi£¬Èç¹uÓеĻ°
     curl_setopt($ch,CURLOPT_URL,$url);
     curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
     curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);  
     //cert Óe key *Ö±ðÊoÓÚÁ½¸o.pemÎļþ
     //ÇeÈ*±£ÄuµÄlibcurl°ae±¾ÊÇ*ñÖ§³ÖË«ÏoÈÏÖ¤£¬°ae±¾¸ßÓÚ7.20.1
     curl_setopt($ch,CURLOPT_SSLCERT,dirname(__FILE__).DIRECTORY_SEPARATOR.'zhengshu'.DIRECTORY_SEPARATOR.'apiclient_cert.pem');
     curl_setopt($ch,CURLOPT_SSLKEY,dirname(__FILE__).DIRECTORY_SEPARATOR.'zhengshu'.DIRECTORY_SEPARATOR.'apiclient_key.pem');
     curl_setopt($ch,CURLOPT_CAINFO,dirname(__FILE__).DIRECTORY_SEPARATOR.'zhengshu'.DIRECTORY_SEPARATOR.'rootca.pem');
     if( count($aHeader) >= 1 ){
      curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
     }
     curl_setopt($ch,CURLOPT_POST, 1);
     curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
     $data = curl_exec($ch);
     if($data){
      curl_close($ch);
      return $data;
     }
     else { 
      $error = curl_errno($ch);
      //echo "call faild, errorCode:$error\n"; 
      curl_close($ch);
      return false;
     }
    }

Èe¿ÚÎļþ£º


    @require "pay.php";
    //»ñÈ¡Óû§ÐÅÏ¢
    $get = $_GET['param'];
    $code = $_GET['code'];
    //ÅжÏcodeÊÇ*ñ´aeÔÚ
    if($get=='access_token' && !empty($code)){
     $param['param'] = 'access_token';
     $param['code'] = $code;
     $packet = new Packet();
     //»ñÈ¡Óû§openidÐÅÏ¢
     $userinfo = $packet->_route('userinfo',$param);
     if(empty($userinfo['openid'])){
      exit("NOAUTH");
     }
     //µ÷È¡Ö§¸¶*½*¨
     $packet->_route('wxpacket',array('openid'=>$userinfo['openid']));
    }else{
     $packet->_route('userinfo');
    }

ÒÔÉϾÍÊǹØÓÚPHP΢Ðźi°uAPI½Ó¿ÚµÄÏeϸ´uÂe£¬*ÖÏi¸ø´o¼Ò£¬Ï£Íu¶Ô´o¼ÒµÄѧϰÓÐËu°iÖu¡£

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
发送邮件
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
提取后缀名
年的日历图
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例