Heim  >  Artikel  >  Backend-Entwicklung  >  Teilen Sie eine gepackte PHP-WeChat-Zahlungsklassenbibliothek (Code-Scanning, H5, Miniprogramm)

Teilen Sie eine gepackte PHP-WeChat-Zahlungsklassenbibliothek (Code-Scanning, H5, Miniprogramm)

藏色散人
藏色散人nach vorne
2021-07-15 14:02:033518Durchsuche

PHP-WeChat-Zahlungsklassenparameter können direkt verwendet werden (Scancode, H5, Miniprogramm)

Wenn wir entsprechende Zahlungen schreiben, verwenden wir häufig WeChat-Zahlungen.

Die Verwendung von WeChat-Zahlungen in PHP ist relativ einfach.

WeChat-Zahlungen Dokument: https://pay.weixin.qq.com/wiki/doc/api/index.html

Hier ist eine kurze Einführung in verschiedene Verwendungsmöglichkeiten der WeChat-Zahlung,

Hier habe ich eine WeChat-Zahlungsklassenbibliothek gekapselt,

kann durch direkte Übergabe der Parameter verwendet werden:

Füllen Sie zunächst die Konfigurationsdatei vollständig aus (achten Sie darauf, sie nicht falsch auszufüllen, da es sonst zu einem Signaturfehler kommt):

$config = array(
    'appid'         => '', // 微信支付appid
    'xcxappid'      => '', // 微信小程序appid
    'mch_id'        => '', // 微信支付 mch_id 商户收款账号
    'key'           => '', // 微信支付key
    'appsecret'     => '', // 公众帐号secert(公众号支付专用)
    'notify_url'    => '', // 接收支付状态的连接  改成自己的回调地址
    'redirect_uri'  => '', // 公众号支付时,没有code,获取openid使用
);

Ich habe daraus auch eine Funktion für gemacht damit verbundene Zahlungen für mehr Benutzerfreundlichkeit,

WeChat-Scancode-Zahlung:

/**
 * [qrcodePay 微信扫码支付]
 * @param  [type] $order [订单信息数组]
 * @return [type]        [description]
 * $order = array(
 *      'body'          => '', // 产品描述
 *      'total_fee'     => '', // 订单金额(分)
 *      'out_trade_no'  => '', // 订单编号
 *      'product_id'    => '', // 产品id(可用订单编号)
 * );
 */
public static function qrcodePay($order=NULL)
{
    if(!is_array($order) || count($order) < 4){
        die("数组数据信息缺失!");
    }
    $order[&#39;trade_type&#39;] = &#39;NATIVE&#39;; // Native支付
    $result = self::unifiedOrder($order);
    $decodeurl = urldecode($result[&#39;code_url&#39;]);
    return $decodeurl; // 使用返回链接直接生成二维码
}

WeChat H5-Zahlung:

/**
 * [weixinH5 微信H5支付]
 * @param  [type] $order [订单信息数组]
 * @return [type]        [description]
 * $order = array(
 *      &#39;body&#39;          => &#39;&#39;, // 产品描述
 *      &#39;total_fee&#39;     => &#39;&#39;, // 订单金额(分)
 *      &#39;out_trade_no&#39;  => &#39;&#39;, // 订单编号
 *      &#39;product_id&#39;    => &#39;&#39;, // 产品id(可用订单编号)
 * );
 */
public static function h5Pay($order=NULL)
{
    if(!is_array($order) || count($order) < 4){
        die("数组数据信息缺失!");
    }
    $order[&#39;trade_type&#39;] = &#39;MWEB&#39;; // H5支付
    $result = self::unifiedOrder($order);
    if ($result[&#39;return_code&#39;]==&#39;SUCCESS&#39; && $result[&#39;result_code&#39;]==&#39;SUCCESS&#39;)
        return $result[&#39;mweb_url&#39;]; // 返回链接让用户点击跳转
    if ($result[&#39;err_code_des&#39;])
        die($result[&#39;err_code_des&#39;]);
    return false;
}

WeChat-Applet-Zahlung:

/**
 * [xcxPay 获取jssdk需要用到的数据]
 * @param  [type]  $order [订单信息数组]
 * @param  boolean $type  [区分是否是小程序,默认 true]
 * @return [type]         [description]
 * $order = array(
 *      &#39;body&#39;          => &#39;&#39;, // 产品描述
 *      &#39;total_fee&#39;     => &#39;&#39;, // 订单金额(分)
 *      &#39;out_trade_no&#39;  => &#39;&#39;, // 订单编号
 *      &#39;product_id&#39;    => &#39;&#39;, // 产品id(可用订单编号)
 *      &#39;openid&#39;        => &#39;&#39;, // 用户openid
 * );
 */
public static function xcxPay($order=NULL,$type=true)
{
    if(!is_array($order) || count($order) < 5){
        die("数组数据信息缺失!");
    }
    $order[&#39;trade_type&#39;] = &#39;JSAPI&#39;; // 小程序支付
    $result = self::unifiedOrder($order,$type);
    if ($result[&#39;return_code&#39;]==&#39;SUCCESS&#39; && $result[&#39;result_code&#39;]==&#39;SUCCESS&#39;) {
        $data = array (
            &#39;appId&#39;     => $type ? $this->config[&#39;xcxappid&#39;] : $this->config[&#39;appid&#39;],
            &#39;timeStamp&#39; => time(),
            &#39;nonceStr&#39;  => self::get_rand_str(32, 0, 1), // 随机32位字符串
            &#39;package&#39;   => &#39;prepay_id=&#39;.$result[&#39;prepay_id&#39;],
            &#39;signType&#39;  => &#39;MD5&#39;, // 加密方式
        );
        $data[&#39;paySign&#39;] = self::makeSign($data);
        return $data; // 数据小程序客户端
    } else {
        if ($result[&#39;err_code_des&#39;])
            die($result[&#39;err_code_des&#39;]);
        return false;
    }
}

Verwendung (hier wird die Applet-Zahlung als Beispiel verwendet):

<?php
include &#39;./WeixinPay.php&#39;;
$get = $_GET;
$weixinpay = new \feng\WeixinPay($config);
$order_sn = time().rand(1000,9999);
$order = array(
    &#39;body&#39;          => &#39;测试商品&#39;, // 产品描述
    &#39;total_fee&#39;     => &#39;1&#39;, // 订单金额(分)
    &#39;out_trade_no&#39;  => $order_sn, // 订单编号
    &#39;product_id&#39;    => $order_sn, // 产品id(可用订单编号)
    &#39;openid&#39;        => $get[&#39;openid&#39;], // 用户openid
);
$re = $weixinpay->xcxPay($order);
die(json_encode($re)); // JSON化直接返回小程序客户端

The Der folgende Code ist eine gekapselte vollständige Zahlungsdatei (WeixinPay .php),

kann nach Belieben entsprechend Ihren eigenen Bedürfnissen geändert werden (Gitee und GitHub werden von Zeit zu Zeit geändert und verbessert):

<?php
/**
 * @Author: [FENG] <1161634940@qq.com>
 * @Date:   2019-09-06 09:50:30
 * @Last Modified by:   [FENG] <1161634940@qq.com>
 * @Last Modified time: 2020-10-08T17:33:39+08:00
 */
namespace feng;
error_reporting(E_ALL);
ini_set(&#39;display_errors&#39;, &#39;1&#39;);
// 定义时区
ini_set(&#39;date.timezone&#39;,&#39;Asia/Shanghai&#39;);
class WeixinPay
{
    // 定义相关配置项
    private static $sslcert_path = &#39;./cert/apiclient_cert.pem&#39;; // 证书(退款时使用)
    private static $sslkey_path = &#39;./cert/apiclient_key.pem&#39;; // 证书(退款时使用)
    private static $referer = &#39;&#39;;
    private static $config = array(
        &#39;appid&#39;         => &#39;&#39;, // 微信支付appid
        &#39;xcxappid&#39;      => &#39;&#39;, // 微信小程序appid
        &#39;mch_id&#39;        => &#39;&#39;, // 微信支付 mch_id 商户收款账号
        &#39;key&#39;           => &#39;&#39;, // 微信支付key
        &#39;appsecret&#39;     => &#39;&#39;, // 公众帐号secert(公众号支付专用)
        &#39;notify_url&#39;    => &#39;&#39;, // 接收支付状态的连接  改成自己的回调地址
        &#39;redirect_uri&#39;  => &#39;&#39;, // 公众号支付时,没有code,获取openid使用
    );
    /**
     * [__construct 构造函数]
     * @param [type] $config [传递微信支付相关配置]
     */
    public function __construct($config=NULL, $referer=NULL){
        $config && self::$config = $config;
        self::$referer = $referer ? $referer : $_SERVER[&#39;HTTP_HOST&#39;];
    }
    /**
     * [unifiedOrder 统一下单]
     * @param  [type]  $order [订单信息(必须包含支付所需要的参数)]
     * @param  boolean $type  [区分是否是小程序,是则传 true]
     * @return [type]         [description]
     * $order = array(
     *      &#39;body&#39;          => &#39;&#39;, // 产品描述
     *      &#39;total_fee&#39;     => &#39;&#39;, // 订单金额(分)
     *      &#39;out_trade_no&#39;  => &#39;&#39;, // 订单编号
     *      &#39;product_id&#39;    => &#39;&#39;, // 产品id
     *      &#39;trade_type&#39;    => &#39;&#39;, // 类型:JSAPI--JSAPI支付(或小程序支付)、NATIVE--Native支付、APP--app支付,MWEB--H5支付
     * );
     */
    public static function unifiedOrder($order, $type=NULL)
    {
        $weixinpay_config = array_filter(self::$config);
        // 获取配置项
        $config = array(
            &#39;appid&#39;             => empty($type) ? $weixinpay_config[&#39;appid&#39;] : $weixinpay_config[&#39;xcxappid&#39;],
            &#39;mch_id&#39;            => $weixinpay_config[&#39;mch_id&#39;],
            &#39;nonce_str&#39;         => &#39;test&#39;,
            &#39;spbill_create_ip&#39;  => self::get_iP(),
            &#39;notify_url&#39;        => $weixinpay_config[&#39;notify_url&#39;]
        );
        $data = array_merge($order, $config); // 合并配置数据和订单数据
        $sign = self::makeSign($data); // 生成签名
        $data[&#39;sign&#39;] = $sign;
        $xml = self::array_to_xml($data);
        $url = &#39;https://api.mch.weixin.qq.com/pay/unifiedorder&#39;;//接收xml数据的文件
        $header[] = "Content-type: text/xml";//定义content-type为xml,注意是数组
        $ch = curl_init ($url);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 兼容本地没有指定curl.cainfo路径的错误
        curl_setopt($ch, CURLOPT_REFERER, self::$referer);        //设置 referer
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
        $response = curl_exec($ch);
        if(curl_errno($ch)){
            die(curl_error($ch)); // 显示报错信息;终止继续执行
        }
        curl_close($ch);
        $result = self::xml_to_array($response);
        if ($result[&#39;return_code&#39;]==&#39;FAIL&#39;)
            die($result[&#39;return_msg&#39;]); // 显示错误信息
        if ($result[&#39;result_code&#39;]==&#39;FAIL&#39;)
            die($result[&#39;err_code_des&#39;]); // 显示错误信息
        $result[&#39;sign&#39;] = $sign;
        $result[&#39;nonce_str&#39;] = &#39;test&#39;;
        return $result;
    }
    /**
     * [qrcodePay 微信扫码支付]
     * @param  [type] $order [订单信息数组]
     * @return [type]        [description]
     * $order = array(
     *      &#39;body&#39;          => &#39;&#39;, // 产品描述
     *      &#39;total_fee&#39;     => &#39;&#39;, // 订单金额(分)
     *      &#39;out_trade_no&#39;  => &#39;&#39;, // 订单编号
     *      &#39;product_id&#39;    => &#39;&#39;, // 产品id(可用订单编号)
     * );
     */
    public static function qrcodePay($order=NULL)
    {
        if(!is_array($order) || count($order) < 4){
            die("数组数据信息缺失!");
        }
        $order[&#39;trade_type&#39;] = &#39;NATIVE&#39;; // Native支付
        $result = self::unifiedOrder($order);
        $decodeurl = urldecode($result[&#39;code_url&#39;]);
        return $decodeurl;
        // qrcode($decodeurl);
        // qrcodeWithPicture($decodeurl);
    }
    /**
     * [jsPay 获取jssdk需要用到的数据]
     * @param  [type] $order [订单信息数组]
     * @return [type]        [description]
     * $order = array(
     *      &#39;body&#39;          => &#39;&#39;, // 产品描述
     *      &#39;total_fee&#39;     => &#39;&#39;, // 订单金额(分)
     *      &#39;out_trade_no&#39;  => &#39;&#39;, // 订单编号
     *      &#39;product_id&#39;    => &#39;&#39;, // 产品id(可用订单编号)
     * );
     */
    public static function jsPay($order=NULL,$code=NULL){
        $config=self::$config;
        if (!is_array($order) || count($order) < 4)
            die("数组数据信息缺失!");
        if (count($order) == 5) {
            $data = self::xcxPay($order, false); // 获取支付相关信息(获取非小程序信息)
            return $data;
        }
        empty($code) && $code = $_GET[&#39;code&#39;];
        // 如果没有get参数没有code;则重定向去获取openid;
        if (empty($code)) {
            $out_trade_no = $order[&#39;out_trade_no&#39;]; // 获取订单号
            $redirect_uri = $config[&#39;redirect_uri&#39;]; // 返回的url
            $redirect_uri = urlencode($redirect_uri);
            $url = &#39;https://open.weixin.qq.com/connect/oauth2/authorize?appid=&#39;.$config[&#39;appid&#39;].&#39;&redirect_uri=&#39;.$redirect_uri.&#39;&response_type=code&scope=snsapi_base&state=&#39;.$out_trade_no.&#39;#wechat_redirect&#39;;
            header(&#39;Location: &#39;.$url);
        } else {
            // 组合获取prepay_id的url
            $url = &#39;https://api.weixin.qq.com/sns/oauth2/access_token?appid=&#39;.$config[&#39;appid&#39;].&#39;&secret=&#39;.$config[&#39;appsecret&#39;].&#39;&code=&#39;.$code.&#39;&grant_type=authorization_code&#39;;
            $result = self::curl_get_contents($url); // curl获取prepay_id
            $result = json_decode($result,true);
            $order[&#39;openid&#39;] = $result[&#39;openid&#39;]; // 获取到的openid
            $data = self::xcxPay($order, false); // 获取支付相关信息(获取非小程序信息)
            return $data;
        }
    }
    /**
     * [xcxPay 获取jssdk需要用到的数据]
     * @param  [type]  $order [订单信息数组]
     * @param  boolean $type  [区分是否是小程序,默认 true]
     * @return [type]         [description]
     * $order = array(
     *      &#39;body&#39;          => &#39;&#39;, // 产品描述
     *      &#39;total_fee&#39;     => &#39;&#39;, // 订单金额(分)
     *      &#39;out_trade_no&#39;  => &#39;&#39;, // 订单编号
     *      &#39;product_id&#39;    => &#39;&#39;, // 产品id(可用订单编号)
     *      &#39;openid&#39;        => &#39;&#39;, // 用户openid
     * );
     */
    public static function xcxPay($order=NULL,$type=true)
    {
        if(!is_array($order) || count($order) < 5){
            die("数组数据信息缺失!");
        }
        $order[&#39;trade_type&#39;] = &#39;JSAPI&#39;; // 小程序支付
        $result = self::unifiedOrder($order,$type);
        if ($result[&#39;return_code&#39;]==&#39;SUCCESS&#39; && $result[&#39;result_code&#39;]==&#39;SUCCESS&#39;) {
            $data = array (
                &#39;appId&#39;     => $type ? self::$config[&#39;xcxappid&#39;] : self::$config[&#39;appid&#39;],
                &#39;timeStamp&#39; => (string)time(),
                &#39;nonceStr&#39;  => self::get_rand_str(32, 0, 1), // 随机32位字符串
                &#39;package&#39;   => &#39;prepay_id=&#39;.$result[&#39;prepay_id&#39;],
                &#39;signType&#39;  => &#39;MD5&#39;, // 加密方式
            );
            $data[&#39;paySign&#39;] = self::makeSign($data);
            return $data; // 数据小程序客户端
        } else {
            if ($result[&#39;err_code_des&#39;])
                die($result[&#39;err_code_des&#39;]);
            return false;
        }
    }
    /**
     * [weixinH5 微信H5支付]
     * @param  [type] $order [订单信息数组]
     * @return [type]        [description]
     * $order = array(
     *      &#39;body&#39;          => &#39;&#39;, // 产品描述
     *      &#39;total_fee&#39;     => &#39;&#39;, // 订单金额(分)
     *      &#39;out_trade_no&#39;  => &#39;&#39;, // 订单编号
     *      &#39;product_id&#39;    => &#39;&#39;, // 产品id(可用订单编号)
     * );
     */
    public static function h5Pay($order=NULL)
    {
        if(!is_array($order) || count($order) < 4){
            die("数组数据信息缺失!");
        }
        $order[&#39;trade_type&#39;] = &#39;MWEB&#39;; // H5支付
        $result = self::unifiedOrder($order);
        if ($result[&#39;return_code&#39;]==&#39;SUCCESS&#39; && $result[&#39;result_code&#39;]==&#39;SUCCESS&#39;)
            return $result[&#39;mweb_url&#39;]; // 返回链接让用户点击跳转
        if ($result[&#39;err_code_des&#39;])
            die($result[&#39;err_code_des&#39;]);
        return false;
    }
    /**
     * [Refund 微信支付退款]
     * @param  [type] $order [订单信息]
     * @param  [type] $type  [是否是小程序]
     * $order = array(
     *      &#39;body&#39;          => &#39;&#39;, // 退款原因
     *      &#39;total_fee&#39;     => &#39;&#39;, // 商品价格(分)
     *      &#39;out_trade_no&#39;  => &#39;&#39;, // 订单编号
     *      &#39;transaction_id&#39;=> &#39;&#39;, // 微信订单号
     * );
     */
    public static function Refund($order, $type=NULL)
    {
        $config = self::$config;
        $data = array(
            &#39;appid&#39;         => empty($type) ? $config[&#39;appid&#39;] : $config[&#39;xcxappid&#39;] ,
            &#39;mch_id&#39;        => $config[&#39;mch_id&#39;],
            &#39;nonce_str&#39;     => &#39;test&#39;,
            &#39;total_fee&#39;     => $order[&#39;total_fee&#39;],         //订单金额     单位 转为分
            &#39;refund_fee&#39;    => $order[&#39;total_fee&#39;],         //退款金额 单位 转为分
            &#39;sign_type&#39;     => &#39;MD5&#39;,                       //签名类型 支持HMAC-SHA256和MD5,默认为MD5
            &#39;transaction_id&#39;=> $order[&#39;transaction_id&#39;],    //微信订单号
            &#39;out_trade_no&#39;  => $order[&#39;out_trade_no&#39;],      //商户订单号
            &#39;out_refund_no&#39; => $order[&#39;out_trade_no&#39;],      //商户退款单号
            &#39;refund_desc&#39;   => $order[&#39;body&#39;],              //退款原因(选填)
        );
        // $unified[&#39;sign&#39;] = self::makeSign($unified, $config[&#39;KEY&#39;]);
        $sign = self::makeSign($data);
        $data[&#39;sign&#39;] = $sign;
        $xml = self::array_to_xml($data);
        $url = &#39;https://api.mch.weixin.qq.com/secapi/pay/refund&#39;;//接收xml数据的文件
        $response = self::postXmlSSLCurl($xml,$url);
        $result = self::xml_to_array($response);
        // 显示错误信息
        if ($result[&#39;return_code&#39;]==&#39;FAIL&#39;) {
            die($result[&#39;return_msg&#39;]);
        }
        $result[&#39;sign&#39;] = $sign;
        $result[&#39;nonce_str&#39;] = &#39;test&#39;;
        return $result;
    }
    /**
     * [notify 回调验证]
     * @return [array] [返回数组格式的notify数据]
     */
    public static function notify()
    {
        $xml = file_get_contents(&#39;php://input&#39;, &#39;r&#39;); // 获取xml
        if (!$xml)
            die(&#39;暂无回调信息&#39;);
        $data = self::xml_to_array($xml); // 转成php数组
        $data_sign = $data[&#39;sign&#39;]; // 保存原sign
        unset($data[&#39;sign&#39;]); // sign不参与签名
        $sign = self::makeSign($data);
        // 判断签名是否正确  判断支付状态
        if ($sign===$data_sign && $data[&#39;return_code&#39;]==&#39;SUCCESS&#39; && $data[&#39;result_code&#39;]==&#39;SUCCESS&#39;) {
            $result=$data;
        }else{
            $result=false;
        }
        // 返回状态给微信服务器
        if ($result) {
            $str=&#39;<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>&#39;;
        }else{
            $str=&#39;<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[签名失败]]></return_msg></xml>&#39;;
        }
        echo $str;
        return $result;
    }
    /**
     * [makeSign 生成签名]
     * 本方法不覆盖sign成员变量,如要设置签名需要调用SetSign方法赋值
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public static function makeSign($data)
    {
        // 去空
        $data = array_filter($data);
        //签名步骤一:按字典序排序参数
        ksort($data);
        $string_a = http_build_query($data);
        $string_a = urldecode($string_a);
        //签名步骤二:在string后加入key
        $config = self::$config;
        $string_sign_temp = $string_a."&key=".$config[&#39;key&#39;];
        //签名步骤三:MD5加密
        $sign = md5($string_sign_temp);
        // 签名步骤四:所有字符转为大写
        $result = strtoupper($sign);
        return $result;
    }
    /**
     * [xml_to_array 将xml转为array]
     * @param  [type] $xml [xml字符串]
     * @return [type]      [转换得到的数组]
     */
    public static function xml_to_array($xml)
    {
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $result = json_decode(json_encode(simplexml_load_string($xml, &#39;SimpleXMLElement&#39;, LIBXML_NOCDATA)), true);
        return $result;
    }
    /**
     * [array_to_xml 输出xml字符]
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public static function array_to_xml($data)
    {
        if(!is_array($data) || count($data) <= 0){
            die("数组数据异常!");
        }
        $xml = "<xml>";
        foreach ($data as $key=>$val){
            if (is_numeric($val)){
                $xml .= "<".$key.">".$val."</".$key.">";
            }else{
                $xml .= "<".$key."><![CDATA[".$val."]]></".$key.">";
            }
        }
        $xml .= "</xml>";
        return $xml;
    }
    /**
     * [curl_get_contents get请求]
     * @param  [type] $url [请求地址]
     * @return [type]      [description]
     */
    public static function curl_get_contents($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);                //设置访问的url地址
        // curl_setopt($ch,CURLOPT_HEADER,1);               //是否显示头部信息
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);               //设置超时
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER[&#39;HTTP_USER_AGENT&#39;]);   //用户访问代理 User-Agent
        curl_setopt($ch, CURLOPT_REFERER, self::$referer);        //设置 referer
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);        //跟踪301
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        //返回结果
        $r=curl_exec($ch);
        curl_close($ch);
        return $r;
    }
    /**
     * [postXmlSSLCurl 需要使用证书的请求]
     * @param  [type]  $xml    [xml数据]
     * @param  [type]  $url    [post请求地址]
     * @param  integer $second [description]
     * @return [type]          [description]
     */
    public static function postXmlSSLCurl($xml,$url,$second=30)
    {
        $ch = curl_init();
        //超时时间
        curl_setopt($ch,CURLOPT_TIMEOUT,$second);
        //这里设置代理,如果有的话
        //curl_setopt($ch,CURLOPT_PROXY, &#39;8.8.8.8&#39;);
        //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
        curl_setopt($ch,CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
        //设置header
        curl_setopt($ch,CURLOPT_HEADER,FALSE);
        //要求结果为字符串且输出到屏幕上
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
        //设置证书
        //使用证书:cert 与 key 分别属于两个.pem文件
        //默认格式为PEM,可以注释
        curl_setopt($ch,CURLOPT_SSLCERTTYPE,&#39;PEM&#39;);
        curl_setopt($ch,CURLOPT_SSLCERT, self::$sslcert_path);
        //默认格式为PEM,可以注释
        curl_setopt($ch,CURLOPT_SSLKEYTYPE,&#39;PEM&#39;);
        curl_setopt($ch,CURLOPT_SSLKEY, self::$sslkey_path);
        //post提交方式
        curl_setopt($ch,CURLOPT_POST, true);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);
        $data = curl_exec($ch);
        //返回结果
        if($data){
            curl_close($ch);
            return $data;
        } else {
            $error = curl_errno($ch);
            echo "curl出错,错误码:$error"."<br>";
            curl_close($ch);
            return false;
        }
    }
    /** fengkui.net
     * [get_rand_str 获取随机字符串]
     * @param  integer $randLength    [长度]
     * @param  integer $addtime       [是否加入当前时间戳]
     * @param  integer $includenumber [是否包含数字]
     * @return [type]                 [description]
     */
    public static function get_rand_str($randLength=6,$addtime=1,$includenumber=0)
    {
        if ($includenumber)
            $chars=&#39;abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQEST123456789&#39;;
        $chars=&#39;abcdefghijklmnopqrstuvwxyz&#39;;
        $len=strlen($chars);
        $randStr=&#39;&#39;;
        for ($i=0;$i<$randLength;$i++){
            $randStr .= $chars[rand(0,$len-1)];
        }
        $tokenvalue = $randStr;
        $addtime && $tokenvalue=$randStr.time();
        return $tokenvalue;
    }
    /** fengkui.net
     * [get_iP 定义一个函数get_iP() 客户端IP]
     * @return [type] [description]
     */
    public static function get_iP()
    {
        if (getenv("HTTP_CLIENT_IP"))
            $ip = getenv("HTTP_CLIENT_IP");
        else if(getenv("HTTP_X_FORWARDED_FOR"))
            $ip = getenv("HTTP_X_FORWARDED_FOR");
        else if(getenv("REMOTE_ADDR"))
            $ip = getenv("REMOTE_ADDR");
        else $ip = "Unknow";
        if(preg_match(&#39;/^((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1 -9]?\d))))$/&#39;, $ip))
            return $ip;
        else
            return &#39;&#39;;
    }
}

Das obige ist der detaillierte Inhalt vonTeilen Sie eine gepackte PHP-WeChat-Zahlungsklassenbibliothek (Code-Scanning, H5, Miniprogramm). Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:fengkui.net. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen