mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-19 12:33:56 +00:00
80 lines
2.3 KiB
PHP
80 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Classes\General;
|
|
|
|
use App\Classes\Exceptions\MalformedRequestException;
|
|
use App\Classes\General\Open1688\SignatureUtil;
|
|
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
|
|
|
|
class Open1688Helper
|
|
{
|
|
|
|
public static function productSearchKeyword($keyword, $page) {
|
|
$requestData = [
|
|
'offerQueryParam' => "{\"keyword\":\"$keyword\",\"beginPage\":$page,\"pageSize\":18,\"country\":\"en\"}",
|
|
];
|
|
|
|
return self::getFromOpen1688($requestData, config('open1688.product_search_keyword_api'));
|
|
}
|
|
|
|
public static function productDetail($offerId) {
|
|
$requestData = [
|
|
'offerDetailParam' => "{\"offerId\":$offerId,\"country\":\"en\"}",
|
|
];
|
|
|
|
return self::getFromOpen1688($requestData, config('open1688.product_detail_api'));
|
|
}
|
|
|
|
private static function getFromOpen1688($requestData, $apiName) {
|
|
$requestData["access_token"] = config('open1688.access_token');
|
|
|
|
$signaturedStr = self::generateRequestSignature($apiName, $requestData);
|
|
$url = config('open1688.url') . $apiName . "/" . config('open1688.app_key') . "?_aop_signature=" . $signaturedStr;
|
|
|
|
|
|
foreach($requestData as $k => $v) {
|
|
$url = $url . "&$k=$v";
|
|
}
|
|
|
|
try {
|
|
$client = new \GuzzleHttp\Client(['verify' => false]);
|
|
$response = $client->request('GET', $url);
|
|
$body = $response->getBody();
|
|
$contents = json_decode($body, true);
|
|
|
|
return $contents['result']['result'];
|
|
} catch(\GuzzleHttp\Exception\RequestException $exception){
|
|
throw new MalformedRequestException(json_decode($exception->getResponse()->getBody()->getContents(), true)['error_message']);
|
|
}
|
|
|
|
}
|
|
|
|
private static function generateRequestSignature ($apiName, $requestData) {
|
|
$pathToSign = self::generateAPIPath($apiName);
|
|
$signaturedStr = SignatureUtil::signature ($pathToSign, $requestData);
|
|
|
|
return $signaturedStr;
|
|
}
|
|
|
|
private static function generateAPIPath($apiName) {
|
|
$urlResult = "";
|
|
$defs = array (
|
|
$urlResult,
|
|
"param2",
|
|
"/",
|
|
"1",
|
|
"/",
|
|
"com.alibaba.fenxiao.crossborder",
|
|
"/",
|
|
$apiName,
|
|
"/",
|
|
config('open1688.app_key')
|
|
);
|
|
|
|
$urlResult = implode ( $defs );
|
|
|
|
return $urlResult;
|
|
}
|
|
|
|
}
|