Files
2026-09-15 10:26:34 +08:00

63 lines
1.7 KiB
PHP

<?php
namespace Stemcell\Sales\Helper;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Magento\Framework\App\Helper\Context;
use GuzzleHttp\ClientFactory;
use GuzzleHttp\Psr7\ResponseFactory;
use Magento\Framework\Exception\LocalizedException;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
const EXPRESS_REQUEST_URI = 'https://kdwlcxf.market.alicloudapi.com/';
const APPCODE = '066aa3e12a754b09a5fa757bfcad91f4';
/**
* @var ClientFactory
*/
protected ClientFactory $clientFactory;
protected ResponseFactory $responseFactory;
public function __construct(
Context $context,
ClientFactory $clientFactory,
ResponseFactory $responseFactory
){
parent::__construct($context);
$this->responseFactory = $responseFactory;
$this->clientFactory = $clientFactory;
}
/**
* @param string $expressNo
* @return array
* @throws LocalizedException
*/
public function getExpressInfo(string $expressNo){
/** @var Client $client */
$client = $this->clientFactory->create(['config' => [
'base_uri' => self::EXPRESS_REQUEST_URI
]]);
try {
$response = $client->request(
'GET',
'kdwlcx/?no='.$expressNo,
['headers'=>['Authorization' => 'APPCODE '.self::APPCODE]]
);
} catch (GuzzleException $exception) {
throw new LocalizedException(__($exception->getMessage()));
}
$result = json_decode($response->getBody()->getContents(), true);
if ($result['status']==0) {
return (array)$result['result'];
}
throw new LocalizedException(__($result['msg']));
}
}