企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
###PHP版本的签名例子 PHP>=5.4 ~~~ <?php class lianhe { public static $version = '2.1'; public static $partnerId = '1111'; private static $apiName = 'lianhe'; private static $clientSecret = 'f1bb800215e2f1a42600ac84e36d6ee3'; private static $url = 'http://www.lianhepiaowu.net/openapi'; private static $instance = null; /** * @return lianhe|null */ public static function create() { if (!isset(self::$instance)) { self::validSign(); self::$instance = new lianhe(); } return self::$instance; } public function request($body, $uri) { date_default_timezone_set('GMT'); $date = date('D, d M Y H:i:s e', time()); date_default_timezone_set('PRC'); $authorization = $this->buildSign('POST', $uri, $date); $header = array( "Content-Type: application/json; charset=utf-8", "Date: " . $date, "Version: " . self::$version, "PartnerId: " . self::$partnerId, "Authorization: " . $authorization, ); $data = array_merge(array( 'errCode' => 200, 'errMsg' => 'success', 'partnerId' => self::$partnerId, ), $body); $data = self::encode_json($data); //可以替换自己的curl工具 $res_json = tool::http_post(self::$url . $uri, $data, $header); $res = json_decode($res_json, true); return $res; } public function outputError($res = '') { if (is_array($res)) { $data = array_merge(array( 'code' => 300, 'partnerId' => self::$partnerId, ),$res); }else{ $data = array( 'code' => 300, 'describe' => $res, 'partnerId' => self::$partnerId, ); } exit(json_encode($data, JSON_UNESCAPED_UNICODE)); } public function outputSucc($res = array(), $desc = 'success') { $data = array_merge(array( 'code' => 200, 'describe' => $desc, 'partnerId' => self::$partnerId, ),$res); exit(json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); } private function validSign() { //todo sign 验证 if($_SERVER['HTTP_AUTHORIZATION'] != self::buildSign( $_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'], $_SERVER['HTTP_DATE'])){ self::outputError('BA验证错误'); } return true; } private function buildSign($method, $uri, $date) { // $string_to_sign = "POST /rhone/lv/deal/change/notice" . "\n" . "Wed, 06 May 2015 10:34:20 GMT"; $string_to_sign = $method . ' ' . $uri . "\n" . $date; $client_secret = self::$clientSecret; $signature = base64_encode(hash_hmac('sha1', $string_to_sign, $client_secret, true)); $authorization = 'LH ' . self::$apiName . ':' . $signature; return $authorization; } } ?> ~~~ PHP<5.4 ~~~PHP <?php class lianhe { public static $version = '2.1'; public static $partnerId = '1111'; private static $apiName = 'lianhe'; private static $clientSecret = 'f1bb800215e2f1a42600ac84e36d6ee3'; private static $url = 'http://www.lianhepiaowu.net/openapi'; private static $instance = null; /** * @return lianhe|null */ public static function create() { if (!isset(self::$instance)) { self::validSign(); self::$instance = new lianhe(); } return self::$instance; } public function request($body, $uri) { date_default_timezone_set('GMT'); $date = date('D, d M Y H:i:s e', time()); date_default_timezone_set('PRC'); $authorization = $this->buildSign('POST', $uri, $date); $header = array( "Content-Type: application/json; charset=utf-8", "Date: " . $date, "Version: " . self::$version, "PartnerId: " . self::$partnerId, "Authorization: " . $authorization, ); $data = array_merge(array( 'errCode' => 200, 'errMsg' => 'success', 'partnerId' => self::$partnerId, ), $body); $data = self::encode_json($data); //可以替换自己的curl工具 $res_json = self::http_post(self::$url . $uri, $data, $header); $res = json_decode($res_json, true); return $res; } /** * POST 请求 * @param string $url * @param array $param * @param array $header * @return string content */ private static function http_post($url, $param, $header = false) { $oCurl = curl_init(); if (stripos($url, "https://") !== FALSE) { curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1 } if (is_string($param)) { $strPOST = $param; } else { $aPOST = array(); foreach ($param as $key => $val) { $aPOST[] = $key . "=" . urlencode($val); } $strPOST = join("&", $aPOST); } if ($header) { curl_setopt($oCurl, CURLOPT_HTTPHEADER, $header); } curl_setopt($oCurl, CURLOPT_URL, $url); curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($oCurl, CURLOPT_POST, true); curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST); $sContent = curl_exec($oCurl); $aStatus = curl_getinfo($oCurl); curl_close($oCurl); if (intval($aStatus["http_code"]) == 200) { return $sContent; } else { return false; } } public function outputError($res = '') { if (is_array($res)) { $data = array_merge(array( 'code' => 300, 'partnerId' => self::$partnerId, ),$res); }else{ $data = array( 'code' => 300, 'describe' => $res, 'partnerId' => self::$partnerId, ); } $name = self::encode_json($data); exit($name); } public function outputSucc($res = array(), $desc = 'success') { $data = array_merge(array( 'code' => 200, 'describe' => $desc, 'partnerId' => self::$partnerId, ),$res); exit( self::encode_json($data)); } private function validSign() { //todo sign 验证 if($_SERVER['HTTP_AUTHORIZATION'] != self::buildSign( $_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'], $_SERVER['HTTP_DATE'])){ self::outputError('BA验证错误'); } return true; } private function buildSign($method, $uri, $date) { // $string_to_sign = "POST /rhone/lv/deal/change/notice" . "\n" . "Wed, 06 May 2015 10:34:20 GMT"; $string_to_sign = $method . ' ' . $uri . "\n" . $date; $client_secret = self::$clientSecret; $signature = base64_encode(hash_hmac('sha1', $string_to_sign, $client_secret, true)); $authorization = 'LH ' . self::$apiName . ':' . $signature; return $authorization; } //中文处理5.4以下 public function encode_json($str) { return urldecode(json_encode(self::url_encode($str) ) ); } function url_encode($str) { if (is_array($str)) { foreach ($str as $key => &$value) { if(is_array($value)){ $value=self::url_encode($value); }else{ (!is_numeric($value))&&($value=urlencode($value)); } } } else { $str = urlencode($str); } return $str; } } ?> ~~~