如何获得shopee api授权使用php5.3

时间:2019-08-20 09:04:17

标签: php api php-5.3

我想使用源代码获得 shopee授权。 我使用的语言是php5.3.2.2.2。 无论我搜索多少次,我都只有一个有关较高版本的演示,因此找不到我想问的答案。

在shopee中,您可以将默认签名序列和伙伴密钥值计算为“ HMAC-SHA256”,并创建一个授权值。如何用代码编写?

我已经获得了合作伙伴密钥。.但是我不知道如何获得授权。

2 个答案:

答案 0 :(得分:0)

为了获得Shopee授权,您需要准备如下所述的标头,并将此标头与其他必需数据一起发送到curl标头中。您可以参考下面给出的示例。

    $secretKey = 'Your Secret Key' ;
    $baseUrl = "https://partner.shopeemobile.com/api/v1/";
    $action = 'The action you want to perform';
    $apiUrl = $baseUrl.$action ;
    $parameters = 'Required Parameters' ;
    $authorisation = $apiUrl."|".json_encode($parmaeters);
    $authorisation = rawurlencode(hash_hmac('sha256', $authorisation, $secretKey, 
    enter code herefalse));

    $header = array(
      'Content-Type: application/json',
      'Authorization: '.$authorisation,
    );

准备好之后,在curl标头中发送$ header

您的CURL请求将类似于此:

    $connection = curl_init();
    curl_setopt($connection, CURLOPT_URL, $apiUrl);
    curl_setopt($connection, CURLOPT_HTTPHEADER, $header);
    curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($connection, CURLOPT_POST, 1);
    curl_setopt($connection, CURLOPT_POSTFIELDS, json_encode($parameters));
    curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);

    $response = curl_exec($connection);
    curl_close($connection);

希望您可以通过此方法解决查询。

答案 1 :(得分:0)

如果你可以在你的 php 应用中使用 npm 包,你可以使用这个代码:

import { createHmac } from 'crypto';
    
const timestamp = getUTCUnixTimestamps();
const baseString = `${AppID}${Apipath}${timestamp}`;
const signCode = createHmac('sha256',shopeeAppSecret).update(baseString).digest('hex');