查询类demo(PHP)

测试环境系统参数及接口调用地址:

$sid         = 'apidev2';
$appkey      = 'xxx2test';
$appsecret   = '12345';

$service_url = 'http://121.41.177.115/openapi2/trade_query.php';
/*trade_query.php此为接口名示例,根据实际对接的接口进行变更*/

正式环境系统参数及接口调用地址:

$sid         = '卖家帐号';
$appkey      = '卖家账号-ot 或 卖家账号-gw';
$appsecret   = 'xxxxxx';
/*appsecret销售向公司申请后提供给商家*/

$service_url = 'http://api.wangdian.cn/openapi2/trade_query.php';
/*trade_query.php此为接口名示例,根据实际对接的接口进行变更*/

查询类demo(PHP)

<?php
//打包请求参数
function packData(&$req)
{
    ksort($req);
    $arr = array();
    foreach($req as $key => $val)
    {
        if($key == 'sign') continue;
        if(count($arr))
            $arr[] = ';';
        $arr[] = sprintf("%02d", iconv_strlen($key, 'UTF-8'));
        $arr[] = '-';
        $arr[] = $key;
        $arr[] = ':';
        $arr[] = sprintf("%04d", iconv_strlen($val, 'UTF-8'));
        $arr[] = '-';
        $arr[] = $val;
    }
    return implode('', $arr);
}
//加密计算sign值
function makeSign(&$req, $appsecret)
{
    $sign = md5(packData($req) . $appsecret);
    $req['sign'] = $sign;
}
//发送请求
function wdtOpenApi($req,$appsecret, $url)
{
    makeSign($req, $appsecret);
    $postdata = http_build_query($req);
    $length   = strlen($postdata);
    $cl       = curl_init($url);
    curl_setopt($cl, CURLOPT_POST, true);
    curl_setopt($cl,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
    curl_setopt($cl,CURLOPT_HTTPHEADER,array("Content-Type: application/x-www-form-urlencoded","Content-length: ".$length));
    curl_setopt($cl,CURLOPT_POSTFIELDS,$postdata);
    curl_setopt($cl,CURLOPT_RETURNTRANSFER,true);
    $content = curl_exec($cl);
    if (curl_errno($cl))
    {
        echo "Error: " . curl_error($cl);
    }
    curl_close($cl);
    $json = json_encode(json_decode($content));
    if(!$json)
    {
        var_dump($content);
        return NULL;
    }
    return $json;
}

$sid         = 'apidev2';
$appkey      = 'xxx2test';
$appsecret   = '12345';

$service_url = 'http://121.41.177.115/openapi2/trade_query.php';
$req = array
(
    'sid' => $sid,
    'appkey' => $appkey,
    'timestamp' => time(),
    'end_time'=>'2018-06-20 11:35:00',
    'start_time'=>'2018-06-20 11:12:00',
    'page_no'=>0,
    'page_size'=>40,
);
$json = wdtOpenApi($req, $appsecret, $service_url);
var_dump($json);
Print Friendly, PDF & Email