| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?php
- namespace app\services\ThirdParty\Taobao;
- use think\facade\Config;
- use think\facade\Log;
- class TaobaoService
- {
- protected $client;
- protected $config;
- protected $adzoneId;
- public function __construct()
- {
- // 从配置文件读取淘宝联盟配置
- $this->config = Config::get('third_party');
- // 引入淘宝SDK
- require_once app()->getRootPath() . 'extend/TaobaoSdk/TopSdk.php';
- $this->client = new \TopClient();
- $this->client->appkey = $this->config['taobao']['appKey'];
- $this->client->secretKey = $this->config['taobao']['appSecret'];
- // 推广位ID
- $this->adzoneId = $this->config['taobao']['adzoneId'];
- }
- /**
- * 执行API请求
- * @param $request
- * @return false|mixed
- */
- protected function execute($request)
- {
- try {
- $response = $this->client->execute($request);
- // var_dump($response);
- return $this->formatResponse($response);
- } catch (\Exception $e) {
- // 记录错误日志
- Log::error('淘宝API请求失败: ' . $e->getMessage());
- return false;
- }
- }
- /**
- * 格式化响应
- * @param $response
- * @return false|mixed
- */
- protected function formatResponse($response)
- {
- if (isset($response->code) && $response->code) {
- // API返回错误
- Log::error("淘宝API错误: {$response->msg} [{$response->code}]");
- return false;
- }
- return $response;
- }
- /**
- * 批量设置对象属性
- * @param object $object 目标对象
- * @param array $params 参数数组
- */
- protected function batchSetProperties($object, array $params)
- {
- foreach ($params as $key => $value) {
- $setter = 'set' . ucfirst($key);
- if (method_exists($object, $setter)) {
- $object->$setter($value);
- }
- }
- }
- }
|