小储云商城下单对接API PHP版

nanxia6个月前学无止境368

该示例用于直接下单,返回成功or失败,无其他功能,根据官方文档简单调写

<?php
    
    //接收参数,这里用$_POST接收,也可以用$_GET接收,不懂勿动
    $id = $_POST['id'] ? $_POST['id'] : $_GET['id']; //你的用户ID
    $num = $_POST['num'] ? $_POST['num'] : $_GET['num']; //购买数量
    $gid = $_POST['gid'] ? $_POST['gid'] : $_GET['gid']; //商品ID
    $value1 = $_POST['value1'] ? $_POST['value1'] : $_GET['value1']; //购买信息1
    $key = $_POST['key'] ? $_POST['key'] : $_GET['key']; //你的密钥
    $url = $_POST['url'] ? $_POST['url'] : $_GET['url']; //你的域名地址
    $qj_url = $_POST['qj_url'] ? $_POST['qj_url'] : $_GET['qj_url']; //小储云地址
    
    
    //下面是对接接口的具体代码
    $DataPost = [
        'act' => 'Docking_buy', //购买的商品
        'url' => $url, //你的域名地址
        'id' => $id, //你的用户ID
        'num' => $num, //购买数量
        'gid' => $gid, //商品ID
        'type' => '1', //付款方式,1余额付款,2积分兑换
        'coupon' => '-2', //可用优惠券ID,-2是自动获取最新,-1是不使用,其它参数可参考优惠券获取接口
        'value1' => $value1, //购买信息1
    
    ];
    
    $DataPost = array_merge([
        'sign' => Sign($DataPost, $key)
    ], $DataPost);
    
    $Data = Curl($qj_url, $DataPost);
    header('Content-type: application/json'); //json
    
    //结果输出
    print_r($Data);
    
    function Sign($param, $key)
    {
        $signPars = '';
        ksort($param);
        foreach ($param as $k => $v) {
            $k = trim($k);
            $v = trim($v);
            if ($k !== 'sign' && $v !== '') {
                $signPars .= $k . '=' . $v . '&';
            }
        }
        $signPars = trim($signPars, '&');
        $signPars .= $key;
        return md5($signPars);
    }
    
    /**
     * @param $url //URL链接
     * @param int $post Post数据
     * @param int $referer 伪造来路域名
     * @param int $cookie CK数据
     * @param int $header 开启请求头
     * @param int $ua 环境模拟
     * @param int $nobaody 输出请求头信息
     * @return bool|string
     * curl请求方法
     */
    function Curl($url, $post = 0, $Header = [], $referer = 0, $cookie = 0, $header = 0, $ua = 0, $nobaody = 0)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $httpheader[] = 'Accept: */*';
        $httpheader[] = 'Accept-Encoding: gzip,deflate,sdch';
        $httpheader[] = 'Accept-Language: zh-CN,zh;q=0.8';
        $httpheader[] = 'Connection: close';
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        if ($post) {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, (is_array($post) ? http_build_query($post) : $post));
        }
        if (count($Header) >= 1) {
            foreach ($Header as $key => $val) {
                $httpheader[] = $key . ': ' . $val;
            }
        }
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
        if ($header) {
            curl_setopt($ch, CURLOPT_HEADER, TRUE);
        }
        if ($cookie) {
            curl_setopt($ch, CURLOPT_COOKIE, $cookie);
        }
        if ($referer) {
            curl_setopt($ch, CURLOPT_REFERER, $referer);
        }
        if ($ua) {
            curl_setopt($ch, CURLOPT_USERAGENT, $ua);
        } else {
            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36');
        }
        if ($nobaody) {
            curl_setopt($ch, CURLOPT_NOBODY, 1);
        }
        curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $ret = curl_exec($ch);
        curl_close($ch);
        return $ret;
    }



相关文章

PHP 常用的header头部定义汇总

header('HTTP/1.1 200 OK'); // ok 正常访问 header('HTTP/1.1 40...

PHP 第三方登录 (QQ登录)过程详解 demo

Step1:获取Authorization Code请求地址 :PC网站:https://graph.qq.com/oauth2.0/authorize请求方法 :GET请求参数&...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。