PHP版滴滴cps联盟Api签名生成和接口调用方法分享

最近准备把我自己开发的淘客系统接入滴滴联盟平台,做打车优惠券搞佣金。
由于淘客系统本身是PHP开发的,所以只能采用PHP的代码接入滴滴联盟Api

下面把滴滴联盟签名生成和接口调用的代码分享给大家

<?php
$AppKey       = "";
$accesskey    = "";
$activity     = 1492;
$promotion_id = "";
$Timestamp    = time();
$source_id    = "source_id";
$generate_link_url = "https://union.didi.cn/openapi/v1.0/link/generate";
$generate_code_url = "https://union.didi.cn/openapi/v1.0/code/generate";

function get_sign($pr, $accesskey)
{
    ksort($pr);
    $ptr = array();
    foreach ($pr as $key => $val) {
        array_push($ptr, $key . "=" . $val);
    }
    $source = urlencode(implode("&", $ptr)).$accesskey;
    echo "source: ".$source."\n";
    $sign = urlencode(base64_encode(sha1($source)));
    return $sign;
}

function post($url, $jsonStr, $header){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

	$parse_url = parse_url($url);
    if($parse_url["scheme"]=="https"){
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
	}
    $response = curl_exec($ch);
    curl_close($ch);
	return $response;
}

function get($url, $params, $header){
    $query = ''; 
    foreach ($params as $param => $value) { 
        $query .= $param.'='.$value .'&'; 
    } 
    $url = $url.'?'.$query;
    $ch = curl_init((string)$url);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $parse_url = parse_url($url);
    if($parse_url["scheme"]=="https"){
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
	}
    $response = curl_exec($ch);
    curl_close($ch);
	return $response;
}

$param_to_sign = array(
    "App-Key"      =>    $AppKey,
    "Timestamp"    =>    $Timestamp,
    "source_id"    =>    $source_id,
    "activity_id"  =>    $activity,
    "link_type"    =>    "h5",
    "promotion_id" =>    $promotion_id
);

$sign = get_sign($param_to_sign, $accesskey);
//由于promotion在json里必须为数字类型,这里用字符串格式化的方式避免被科学计数法。我不是专业的php程序员,如有合理的方法,欢迎向我们提供
$json = sprintf("{\"activity_id\":%d,\"source_id\":\"%s\",\"link_type\":\"%s\",\"promotion_id\":%s}", $activity, $source_id, "h5", $promotion_id);

$header = array(
    "App-Key: ".$AppKey,
    "Timestamp: ". $Timestamp,
    "Sign: ".$sign,
    "Content-Type: application/json",
    "Content-Length: ".strlen($json)
);
//请求生成短链
$link_response = json_decode(post($generate_link_url, $json, $header), true);
print_r($link_response);
if ($link_response["errno"] != 0) {
    return;
}
$dsi = $link_response["data"]["dsi"];

$param_to_sign = array(
    "App-Key"      =>    $AppKey,
    "Timestamp"    =>    $Timestamp,
    "source_id"    =>    $source_id,
    "dsi"          =>    $dsi,
    "type"         =>    "mini"
);

$param = array(
    "source_id"    =>    $source_id,
    "dsi"          =>    $dsi,
    "type"         =>    "mini"
);

$sign = get_sign($param_to_sign, $accesskey);
$header = array(
    "App-Key: ".$AppKey,
    "Timestamp: ". $Timestamp,
    "Sign: ".$sign
);
echo get($generate_code_url, $param, $header);

?>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值