代码改变世界

kaufland平台api

2023-02-28 17:31  天心PHP  阅读(66)  评论(1编辑  收藏  举报
 class KauflandapiController extends YbController{


     public $header, $url, $secretKey, $ClientKey;

     /**
      * GetDiscussionMailListResponse constructor.
      * @param $response
      */
     public function __construct()
     {
         $this->secretKey = "2ac36025d9dbf5a1c2e0869b8a2e";
         $this->ClientKey = "b2c4666dc9cf64b1cf29";
         $header = array();
         $header[] = 'Accept:application/json';
         $header[] = 'Content-Type:application/json';
         $header[] = 'HM-Client:'.$this->ClientKey;
         $this->header = $header;
         $this->url = "https://www.kaufland.de";
     }

     /**
      * description: 所有需要用到的调用接口地址信息统一管理
      */
     public function getUrl($url_code)
     {
         $urls = [
             'categories' => '/api/v1/categories',
             'update_product' => '/api/v1/product-data/',
             'report_list' => '/api/v1/reports/seller',
             'report_info' => '/api/v1/reports/',
             'account_listing' => '/api/v1/reports/account-listing',
             'get_units_listing' => '/api/v1/units/seller',
             'get_units' => '/api/v1/units/',
         ];
         return $this->url ? $this->url . $urls[$url_code] : '';
     }

     /**
      * description: 所有需要用到的调用接口地址信息
      */
     public function signRequest($method, $uri, $body, $timestamp, $secretKey)
     {
         $string = implode("\n", [
             $method,
             $uri,
             $body,
             $timestamp,
         ]);
         echo $string;
         return hash_hmac('sha256', $string, $secretKey);
     }

     /**
      * description: 分类接口内容调用
      * @throws CException
      * url:/services/yunyi/Kauflandapi/categorier
      */
     public function actionCategorier()
     {
         $uri = $this->getUrl("categories");
         $time = time();
         $hash_sign = $this->signRequest("GET", $uri, "", $time , $this->secretKey);
         $header = $this->header;
         $header[] = "HM-Signature:".$hash_sign;
         $header[] = "HM-Timestamp:".$time;
         //$result = $this->cur_get($uri,$header);
         $result = $this->cur_request($uri,"GET","",$header);
         echo "<pre>";
            print_r($result);
            exit;
     }

     /**
      * description: 获取报告列表
      * @throws CException
      * url:/services/yunyi/Kauflandapi/getreportlist
      */
     public function actionGetreportlist()
     {
         $uri = $this->getUrl("report_list");
         $time = time();
         $hash_sign = $this->signRequest("GET", $uri, "", $time , $this->secretKey);
         $header = $this->header;
         $header[] = "HM-Signature:".$hash_sign;
         $header[] = "HM-Timestamp:".$time;
         //$result = $this->cur_get($uri,$header);
         $result = $this->cur_request($uri,"GET","",$header);
         echo "<pre>";
         echo $uri."\n";
         print_r($result);
         exit;
     }

     /**
      * description: 获取报告信息
      * @throws CException
      * url:/services/yunyi/Kauflandapi/getreportinfo
      */
     public function actionGetreportinfo()
     {
         $report_id = "4041098";
         $uri = $this->getUrl("report_info").$report_id;
         $time = time();
         $hash_sign = $this->signRequest("GET", $uri, "", $time , $this->secretKey);
         $header = $this->header;
         $header[] = "HM-Signature:".$hash_sign;
         $header[] = "HM-Timestamp:".$time;
         //$result = $this->cur_get($uri,$header);
         $result = $this->cur_request($uri,"GET","",$header);
         echo "<pre>";
         echo $uri."\n";
         print_r($result);
         exit;
     }

     /**
      * description: 获取账号listing 信息
      * @throws CException
      * url:/services/yunyi/Kauflandapi/getaccountlist
      */
     public function actionGetaccountlist()
     {
         $uri = $this->getUrl("account_listing");
         $time = time();
         $hash_sign = $this->signRequest("POST", $uri, "", $time , $this->secretKey);
         $header = $this->header;
         $header[] = "HM-Signature:".$hash_sign;
         $header[] = "HM-Timestamp:".$time;
         //$result = $this->cur_get($uri,$header);
         $result = $this->cur_request($uri,"POST","",$header);
         echo "<pre>";
         echo $uri."\n";
         print_r($result);
         exit;
     }

     /**
      * description: 获取单个商品信息
      * @throws CException
      * url:/services/yunyi/Kauflandapi/getproduct
      */
     public function actionGetproduct()
     {
         $ean = "9062609283155";
         $uri = $this->getUrl("update_product").$ean;
         $time = time();
         $hash_sign = $this->signRequest("GET", $uri, "", $time , $this->secretKey);
         $header = $this->header;
         $header[] = "HM-Signature:".$hash_sign;
         $header[] = "HM-Timestamp:".$time;
         //$result = $this->cur_get($uri,$header);

         $result = $this->cur_request($uri,"GET","",$header);
         echo "<pre>";
         echo $uri."\n";
         print_r($result);
         exit;
     }

     /**
      * description: 获取单个unit信息
      * @throws CException
      * url:/services/yunyi/Kauflandapi/getunitinfo
      */
     public function actionGetunitinfo()
     {
         $id_unit = "384536239807";
         $uri = $this->getUrl("get_units").$id_unit;
         $time = time();
         $hash_sign = $this->signRequest("GET", $uri, "", $time , $this->secretKey);
         $header = $this->header;
         $header[] = "HM-Signature:".$hash_sign;
         $header[] = "HM-Timestamp:".$time;
         //$result = $this->cur_get($uri,$header);
         $result = $this->cur_request($uri,"GET","",$header);
         echo "<pre>";
         echo $uri."\n";
         print_r($result);
         exit;
     }

     /**
      * description: 更新单个unit信息
      * @throws CException
      * url:/services/yunyi/Kauflandapi/updateunits
      */
     public function actionUpdateunits()
     {
         $id_unit = "384536239807";
         $uri = $this->getUrl("get_units").$id_unit;;
         $time = time();
         $body = [
             "condition"=>"new",
             "listing_price"=>6392,
             "minimum_price"=>6392,
             "amount"=> 200,
             "id_offer"=> "00001670-01",
             "note"=> "",
             "delivery_time"=> "e",
             "delivery_time_min"=> 4,
             "delivery_time_max"=> 20,
             "shipping_group"=> "jiekecang",
             "location"=> "CN",
             "warehouse"=> "Shenzhenshi runxi keji",
             "reference_price"=> 0
         ];
         //$uri = $uri."?id_unit=$id_unit";
         $body_jsonstr = json_encode($body);
         $hash_sign = $this->signRequest("PATCH", $uri, $body_jsonstr, $time , $this->secretKey);
         $header = $this->header;
         $header[] = "HM-Signature:".$hash_sign;
         $header[] = "HM-Timestamp:".$time;
         //$result = $this->cur_get($uri,$header);
         $result = $this->cur_request($uri,"PATCH",$body_jsonstr,$header);
         echo "<pre>";
         echo $uri."\n";
         print_r($header);
         print_r($result);
         exit;
     }

     /**
      * description: 获取unit listing信息
      * @throws CException
      * url:/services/yunyi/Kauflandapi/getunitlisting
      */
     public function actionGetunitlisting()
     {
         $uri = $this->getUrl("get_units_listing");
         $time = time();
         $body["id_offer"] = "3505-006140-09U0A";
         $body["id_item"] = 2147483647;
         $body["ean"] = "9072609509223";
         $body["limit"] = 300;
         $body["offset"] = 1;
         $body["embedded"] = "item";
         //$url=$uri."?limit=30&offset=0&id_offer=3505-006140-09U0A&ean=9072609509223";
         $url=$uri."?limit=30&offset=1";
         $hash_sign = $this->signRequest("GET", $url, "", $time , $this->secretKey);
         $header = $this->header;
         $header[] = "HM-Signature:".$hash_sign;
         $header[] = "HM-Timestamp:".$time;
         //$result = $this->cur_get($uri,$header);
         $result = $this->cur_request($url,"GET","",$header);
         echo "<pre>";
         echo $uri."\n";
         print_r($header);
         print_r($result);
         exit;
     }

     /**
      * description: 进行更新产品数据
      * @throws CException
      * url:/services/yunyi/Kauflandapi/updateproduct
      */
     public function actionUpdateproduct()
     {
         $ean = "";
         $uri = $this->getUrl("categories").$ean;
         $time = time();
         $params = [
             "ean"=>[""],
             "title"=>[""],
             "category"=>[""],
             "additional_categories"=>[""],
             "description"=>[""],
             "short_description"=>[""],
             "mpn"=>[""],
             "list_price"=>[""],
             "picture"=>[""],
             "additional_attributes"=>[["attribute"=>"","value"=>[""]]],
         ];
         echo json_encode($params);exit;
         $hash_sign = $this->signRequest("PATCH", $uri, $body, $time , $this->secretKey);
         $header = $this->header;
         $header[] = "HM-Signature:".$hash_sign;
         $header[] = "HM-Timestamp:".$time;
         //$result = $this->cur_get($uri,$header);
         $result = $this->cur_request($uri,"PATCH","",$header);
         echo "<pre>";
         print_r($result);
         exit;
     }

    // 请求方法的封装
     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/json'));
         }
         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)];
     }

     //POST 请求数据
     public function cur_post($url, $post_data, $header)
     {
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, $url);
         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
         curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
         curl_setopt($ch, CURLOPT_POST, 1);
         if (!$post_data) {
             $post_data = "{}";
         }
         curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         $response = curl_exec($ch);
         if (curl_errno($ch)) {
             return array("ack" => 0, "result" => "ERROR:" . curl_error($ch));
         }
         curl_close($ch);
         return json_decode($response,true);
     }

     //GET 请求数据
     public function cur_get($url,$header)
     {
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, $url);
         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
         curl_setopt($ch, CURLOPT_TIMEOUT, 30);
         curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
         curl_setopt($ch, CURLOPT_POST, 0);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         $response = curl_exec($ch);
         if (curl_errno($ch)) {
             return array("ack" => 0, "result" => "ERROR:" . curl_error($ch));
         }
         curl_close($ch);
         return json_decode($response,true);
     }
 }