PHP code example of lingxing / openapi

1. Go to this page and download the library: Download lingxing/openapi library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lingxing / openapi example snippets


/**
 * host为OpenAPI域名,需要带上协议,如 https://openapi.lingxing.com
 * appId则为开发者的appId
 * appSecret为开发者的appSecret
 */
$client = new \Ak\OpenAPI\Services\OpenAPIRequestService('host', 'appId', 'appSecret');
/**
 * 发起请求前需要先生成AccessToken或手动设置AccessToken,否则会抛出 InvalidAccessTokenException
 * AccessToken有时效性,可以自行加入缓存,并判断是否已过期,方便续约或重新生成
 */
$accessTokenDto = $client->generateAccessToken();

/**
 * 获取AccessToken
 */
 $accessTokenDto->getAccessToken();
 
 /**
 * 获取RefreshToken(用于刷新AccessToken),请自行保存好
 */
 $accessTokenDto->getRefreshToken();

/**
 * 获取过期时间戳,请自行保存好,用于判断AccessToken是否已过期
 */
$accessTokenDto->getExpireAt();
 
 /**
  * 刷新AccessToken,AccessToken到期前需续约,这里请自行判断AccessToken的有效期
 */
 $client->refreshToken($accessTokenDto->getRefreshToken());

/**
 * 手动设置AccessToken
 */
 $accessToken = 'get_access_token_from_cache';
 $client->setAccessToken($accessToken);
 

/**
 * GET 请求示例
 * $res 会是一个数组,接口文档返回结果json_decode()后的数组结果
 */
$res = $client->makeRequest('/erp/sc/data/seller/lists', 'GET');

/**
 * POST 请求示例
 */
$params = ['start_date'=>'2023-07-18 00:00:00','end_date'=>'2023-08-18 23:59:59'];
$res = $client->makeRequest('/erp/sc/data/mws/orders', 'POST', $params);
shell
composer dump-autoload