| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- class Model_apittv1 extends Lin_Model {
- function __construct(){
- parent::__construct();
- $this->load->_model("Model_logic_ding","logic_ding");
- }
- private function sign($url,$header,$body,$secret){
- //解析URL
- $urlParts = parse_url($url);
- //获取路径
- $path = $urlParts['path']??'';
- //解析查询参数
- $queryParams = [];
- if(isset($urlParts['query'])){
- parse_str($urlParts['query'],$queryParams);
- }
- // 移除sign和access_token参数
- unset($queryParams['sign']);
- unset($queryParams['access_token']);
-
- // 按参数名排序
- ksort($queryParams);
- // 构建参数字符串
- $parameterStr = $path;
- foreach ($queryParams as $key => $value) {
- $parameterStr .= $key . $value;
- }
- // 检查Content-Type,如果不是multipart/form-data,则追加body
- $contentType = $this->getHeader($header, 'Content-Type');
- if (stripos($contentType, 'multipart/form-data') === false && !empty($body)) {
- $parameterStr .= $body;
- }
-
- // 用secret包裹参数字符串
- $signatureParams = $secret . $parameterStr . $secret;
-
- // 使用HMAC-SHA256生成签名
- return $this->generateSHA256($signatureParams, $secret);
- }
- private function getHeader($header,$name){
- $lowerName = strtolower($name);
- foreach ($header as $key => $value) {
- if (strtolower($key) === $lowerName) {
- return $value;
- }
- }
- return '';
- }
- private function generateSHA256($signatureParams, $secret){
- return hash_hmac('sha256', $signatureParams, $secret);
- }
- private function reqGet($url,$headers){
-
- // 初始化cURL
- $ch = curl_init();
-
- // 设置cURL选项
- curl_setopt_array($ch, [
- CURLOPT_URL => $url,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_HTTPGET => true, // 明确设置为GET请求
- CURLOPT_HTTPHEADER => $headers,
-
- // SSL设置 - 禁用验证(仅测试环境)
- CURLOPT_SSL_VERIFYPEER => false,
- CURLOPT_SSL_VERIFYHOST => false,
-
- // 其他设置
- CURLOPT_TIMEOUT => 300,
- CURLOPT_FOLLOWLOCATION => true,
- ]);
-
- // 执行请求
- $response = curl_exec($ch);
-
- // 检查错误
- $error_msg = "";
- if (curl_errno($ch)) {
- $error_msg = $ch;
- }
-
- // 获取HTTP状态码
- $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
-
- return [
- 'httpCode'=>$httpCode,
- 'response'=>$response,
- 'errorMsg'=>$error_msg
- ];
-
- }
- private function reqPost($url,$headers,$data){
- // 初始化cURL
- $ch = curl_init();
- // 设置cURL选项
- curl_setopt_array($ch, [
- CURLOPT_URL => $url,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_POST => true,
- CURLOPT_HTTPHEADER => $headers,
- CURLOPT_POSTFIELDS => json_encode($data),
- CURLOPT_SSL_VERIFYPEER => false, // 根据实际情况调整,生产环境建议设为true
- CURLOPT_TIMEOUT => 300,
- ]);
- // 执行请求
- $response = curl_exec($ch);
- // 检查错误
- $error_msg = "";
- if (curl_errno($ch)) {
- $error_msg = curl_error($ch);
- }
- // 获取HTTP状态码
- $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- // 关闭cURL资源
- curl_close($ch);
- return [
- 'httpCode'=>$httpCode,
- 'response'=>$response,
- 'errorMsg'=>$error_msg
- ];
- }
- public function downloadLabel($package_id,$shop,$info){
- $url = 'https://open-api.tiktokglobalshop.com';
- $link = '/fulfillment/202309/packages/'.$package_id.'/shipping_documents';
- $time = time();
- $link .= '?shop_cipher='.$shop['shop_cipher'].'&app_key='.$shop['app_key'].'×tamp='.$time."&document_type=SHIPPING_LABEL";
- $url = $url.$link;
- $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
- $sign = $this->sign($url,$headers,[],$shop['app_key']);
- $url .= $link.'&sign='.$sign;
- $res = $this->reqGet($url,$headers);
- if($res['httpCode'] != 200){
- echo "<pre>";
- print_r($res);
- }else{
- echo "<pre>";
- print_r($res);
- }
-
- }
-
- }
|