代码改变世界

Allegro API接口

2025-04-23 15:12  天心PHP  阅读(37)  评论(0)    收藏  举报
Allegro退货报告  文档:https://developer.allegro.pl/documentation#operation/getCustomerReturns
AllegroController
<?php
class AllegroController extends YbController{
    /*
     * /services/allegro/allegro/getcustomerreturns
     */
    public function actionGetcustomerreturns()
    {
        $accountId= 11;
        $accmodel = new AllegroAccount();
        $accinfo = $accmodel->getAccountInfoByAccountId($accountId);
        $accres =  new AllegroModel($accinfo);
        $end_date = date('Y-m-d\TH:i:s', strtotime(date('Y-m-d'))); //当天
        $end_utcDate = $end_date . 'Z'; // 添加'Z'表示UTC
        $start_date = date('Y-m-d\TH:i:s', strtotime(date('Y-m-d', strtotime('2025-01-01')))); // 转换为日期时间格式,但不包括毫秒和时区
        $start_utcDate = $start_date . 'Z'; // 添加'Z'表示UTC
        $data['createdAt.gte'] = $start_utcDate;
        $data['createdAt.lte'] = $end_utcDate;
        $data['limit'] = 1000;
        $data['offset'] = 0;
        $res = $accres->get_customer_returns($data);
        print_r('<pre>');
        print_r($res);
        print_r('</pre>');
        exit();
    }
}
AllegroModel
<?php
class AllegroModel extends YbModel {

    protected $access_token = null;
    protected $client_id = null;
    protected $client_secret = null;
    protected $host_url = null;
    /**
     * db config key
     *
     * @return string
     */
    public function getDbKey() {
        return 'db';
    }

    public function __construct($account){
        $this->host_url = 'https://api.allegro.pl/';
        $this->access_token = $account['access_token'];
        $this->client_id = $account['client_id'];
        $this->client_secret = $account['client_secret'];
    }

    public function get_customer_returns($data)
    {
        foreach ($data as $key=>$val){
            $url.=$key.'='.$val.'&';
        }
        $params = rtrim($url,"&");
        $url = $this->host_url.'order/customer-returns';
        if($params){
            $url = $url.'?'.$params;
        }
        $headers[] = "Accept: application/vnd.allegro.beta.v1+json";
        $headers[] = "Authorization: Bearer " . $this->access_token;
        $headers[] = "Content-Type: application/vnd.allegro.beta.v1+json";
        $res = $this->cur_request($url, 'GET', '', $headers);
        return $res;
    }

    public function cur_request($URL, $type, $params, $headers)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $URL);
        if ($headers != "") {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        } else {
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/x-www-form-urlencoded'));
        }
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        switch ($type) {
            case "GET" :
                curl_setopt($ch, CURLOPT_HTTPGET, true);
                break;
            case "POST":
                curl_setopt($ch, CURLOPT_POST, true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
                break;
            case "PUT" :
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
                curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
                break;
            case "PATCH":
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
                curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
                break;
            case "DELETE":
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
                curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
                break;
        }
        $file_contents = curl_exec($ch);//获得返回值
        $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        return [$responseCode, json_decode($file_contents, true)];
    }
}