大家好,欢迎来到IT知识分享网。
下面把滴滴联盟签名生成和接口调用的代码分享给大家
<?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); ?>
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/150954.html