PHP版滴滴cps联盟Api签名生成和接口调用方法分享
作者:mmseoamin日期:2023-12-25

最近准备把我自己开发的淘客系统接入滴滴联盟平台,做打车优惠券搞佣金。

由于淘客系统本身是PHP开发的,所以只能采用PHP的代码接入滴滴联盟Api

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

 $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);
?>