代码改变世界

Ozon API接口

2025-04-23 15:07  天心PHP  阅读(283)  评论(0)    收藏  举报

退货申请列表

https://docs.ozon.ru/api/seller/zh/?__rr=1#operation/RFBSReturnsAPI_ReturnsRfbsListV2

OzonController

<?php
class OzonController extends YbController
{
    /**
     * @Notes : 获取Ozon退货报告
     * /services/ozon/ozon/getgoodsreturn
     * @author: xsh
     * @Time  : 2025/4/22 0020   14:40
     */
    public function actionGetgoodsreturn()
    {
        set_time_limit('3600');
        $account_id = Yii::app()->request->getParam('account_id');
        $type = Yii::app()->request->getParam('type');
        $gstart_date = Yii::app()->request->getParam('start_date');
        $gend_date = Yii::app()->request->getParam('end_date');
        $model = new Ozonreturns();
        $accmodel = new OzonAccount();
        $keys = "ozon_returns_line";
        if($type=='see'){
            $accountss = Yii::app()->memcache->get($keys);
            $lines = json_decode($accountss,true);
            VHelper::dump($lines);exit();
        }
        if($type=='del'){
            Yii::app()->memcache->delete($keys);
            die('删除换成成功');
        }
        if($account_id){
            if($gstart_date){
                $start_date = date('Y-m-d\TH:i:s', strtotime(date('Y-m-d', strtotime($gstart_date)))); // 转换为日期时间格式,但不包括毫秒和时区
            }else{
                $start_date = date('Y-m-d\TH:i:s', strtotime(date('Y-m-d', strtotime('-31 day')))); // 转换为日期时间格式,但不包括毫秒和时区
            }
            $start_utcDate = $start_date . 'Z'; // 添加'Z'表示UTC
            if($gend_date){
                $end_date = date('Y-m-d\TH:i:s', strtotime(date('Y-m-d', strtotime($gend_date)))); // 转换为日期时间格式,但不包括毫秒和时区
            }else{
                $end_date = date('Y-m-d\TH:i:s', strtotime(date('Y-m-d', strtotime('-1 day')))); // 转换为日期时间格式,但不包括毫秒和时区
            }
            $end_utcDate = $end_date . 'Z'; // 添加'Z'表示UTC
            $oaccmodel=new OzonAccount();
            $accountinfo = $oaccmodel->findByPk($account_id);
            $wmodel = new Ozon($accountinfo);
            $datetimes = date('Y-m-d H:i:s');
            $paramdata['filter'] = ['created_at'=>['from'=>$start_utcDate,'to'=>$end_utcDate]];
            $paramdata['limit'] = 1000;
            $res = $wmodel->get_goods_return($paramdata);
            if($res[0]==200 && isset($res[1]['returns'])){
                $list = $data = [];
                foreach ($res[1]['returns'] as $key => $value) {
                    $dateTime = new DateTime($value['created_at']);
                    $created_date = $dateTime->format('Y-m-d H:i:s');
                    $data['account_name'] = $accountinfo->account_name;
                    $data['account_id'] = $account_id;
                    $data['batchnumber'] = date('Ym',strtotime($created_date));
                    $data['client_name'] = $value['client_name'];
                    $data['created_at'] = $value['created_at'];
                    $data['created_date'] = $created_date;
                    $data['order_number'] = $value['order_number'];
                    $data['posting_number'] = $value['posting_number'];
                    $data['product_name'] = $value['product']['name'];
                    $data['product_offer_id'] = $value['product']['offer_id'];
                    $data['product_currency_code'] = $value['product']['currency_code'];
                    $data['product_price'] = $value['product']['price'];
                    $data['product_sku'] = $value['product']['sku'];
                    $data['return_id'] = $value['return_id'];
                    $data['return_number'] = $value['return_number'];
                    $data['group_state'] = $value['state']['group_state'];
                    $data['money_return_state_name'] = $value['state']['money_return_state_name'];
                    $data['state'] = $value['state']['state'];
                    $data['state_name'] = $value['state']['state_name'];
                    $infoid = $model->getDbConnection()->createCommand()->select('id')->from("yibai_ozon_returns")
                        ->where("order_number='{$value['order_number']}' and return_id={$value['return_id']}")->queryScalar();
                    if($infoid){
                        $data['update_date'] = $datetimes;
                        $model->updateAll($data,"id=".$infoid);
                    }else{
                        $data['create_date'] = $datetimes;
                        $list[] = $data;
                        if(count($list)>=2000){
                            $fart = $model->batchReplaceAll("{{ozon_returns}}", array_keys($list[0]), $list);
                            if($fart){
                                $list = [];
                            }
                        }
                    }
                }
                if($list){
                    $fart = $model->batchReplaceAll("{{ozon_returns}}", array_keys($list[0]), $list);
                    if($fart){
                        $list = [];
                    }
                }
            }
            $accountss = Yii::app()->memcache->get($keys);
            $accountss = json_decode($accountss,true);
            $line = $account_id%10;
            if(!empty($accountss[$line])){
                $account_id = array_shift($accountss[$line]);
                Yii::app()->memcache->set($keys,json_encode($accountss),12*3600);
                if($gstart_date || $gend_date){
                    $url = sprintf('%s/services/ozon/ozon/getgoodsreturn/account_id/%s/start_date/%s/end_date/%s', $_SERVER['HTTP_HOST'], $account_id,$start_date,$end_date);
                }else{
                    $url = sprintf('%s/services/ozon/ozon/getgoodsreturn/account_id/%s', $_SERVER['HTTP_HOST'], $account_id);
                }
                echo $url;echo '<br/>';
                MHelper::curl_post_async($url);
            }else{
                $line = $line+1;
                if($line>9){
                    die('处理完成');
                }
                $account_id = array_shift($accountss[$line]);
                Yii::app()->memcache->set($keys,json_encode($accountss),12*3600);
                if($gstart_date || $gend_date){
                    $url = sprintf('%s/services/ozon/ozon/getgoodsreturn/account_id/%s/start_date/%s/end_date/%s', $_SERVER['HTTP_HOST'], $account_id,$start_date,$end_date);
                }else{
                    $url = sprintf('%s/services/ozon/ozon/getgoodsreturn/account_id/%s', $_SERVER['HTTP_HOST'], $account_id);
                }
                echo $url;echo '<br/>';
                MHelper::curl_post_async($url);
            }
        }else{
            $accountss = Yii::app()->memcache->get($keys);
            $lines = json_decode($accountss,true);
            if(count(array_filter($lines))==0){
                Yii::app()->memcache->delete($keys);
            }
            if(empty($lines)){
                $accounts = $accmodel->getDbConnection()->createCommand()
                    ->select('id,MOD(id,10)as line')
                    ->from($accmodel->tableName())
                    ->where('status=1')
                    ->order('id asc')
                    ->queryAll();
                if(!$accounts){
                    die('数据已经处理完成');
                }
                $accounts_line=array_column($accounts,null,'id');
                foreach($accounts_line as $val){
                    $lines[$val['line']][] = $val['id'];
                }
            }
            if(count(array_filter($lines))==0){
                die('处理完成');
            }
            for($i=0;$i<10;$i++){
                if(!empty($lines[$i])){
                    $account_id = array_shift($lines[$i]);
                    Yii::app()->memcache->set($keys,json_encode($lines),12*3600);
                    if($gstart_date || $gend_date){
                        $url = sprintf('%s/services/ozon/ozon/getgoodsreturn/account_id/%s/start_date/%s/end_date/%s', $_SERVER['HTTP_HOST'], $account_id,$gstart_date,$gend_date);
                    }else{
                        $url = sprintf('%s/services/ozon/ozon/getgoodsreturn/account_id/%s', $_SERVER['HTTP_HOST'], $account_id);
                    }
                    echo $url;echo '<br/>';
                    MHelper::curl_post_async($url);
                    break;
                }else{
                    continue;
                }
            }
        }
    }

}
class Ozon
<?php
class Ozon
{
    private $Client_Id;
    private $Api_Key;
    private $host_url;
    function __construct($account){
        //获取配置信息
        $this->Client_Id = $account->client_id;
        $this->Api_Key = $account->api_key;
        $this->host_url = 'https://api-seller.ozon.ru';
    }

    public function get_goods_return($paramdata)
    {
        $url = '/v2/returns/rfbs/list';
        $url = $this->host_url.$url."";
        $headers[] = 'Client-Id:' . $this->Client_Id;
        $headers[] = 'Api-Key:' . $this->Api_Key;
        $headers[] = 'Content-Type: application/json';
        $res = $this->cur_request($url, 'POST', json_encode($paramdata), $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)];
    }
}