瀏覽代碼

php开发示例

duanzonglong 6 年之前
父節點
當前提交
5ff050e67e
共有 4 個文件被更改,包括 320 次插入0 次删除
  1. 4 0
      README.md
  2. 132 0
      php/Qimen.php
  3. 151 0
      php/SingleitemSynchronizeRequest.php
  4. 33 0
      php/test.php

+ 4 - 0
README.md

@@ -32,6 +32,10 @@ StockOrderTest
 ## 2018.4.26更新说明
 商品接口添加包装率
 
+## 2018.5.14更新说明
+添加php开发示例 php/test.php
+
+
 ### 开始联调
 + 找运维拿到自己的appkey secret customerId 货主编码-ownerCode 仓库编码-warehouseCode 
 + 以商品接口举例修改 SingleItemTest

+ 132 - 0
php/Qimen.php

@@ -0,0 +1,132 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: zhangtao
+ * Date: 2018/5/14
+ * Time: 下午3:48
+ */
+
+class Qimen
+{
+    private $secret = 'RA8wjgCNocNo99IAd5wFFW93Wll1TuRC';          // 奇门提供的安全码(签名用)
+
+    private $param = array(                      // 按接口提供的逐一填写
+        "format" => 'xml',
+        "app_key" => '201804261190',
+        "v" => '2.0',
+        "sign_method" => 'md5',
+        "customerId" => 'lt',
+    );
+
+    /**
+     * 签名
+     * @param $secret  安全码
+     * @param $param   提交参数
+     * @param $body     提交文档内容
+     */
+    public function sign($secret, $param, $body)
+    {
+        if (empty($body)) {
+            exit('Body can\'t empty!');
+        }
+
+        if (empty($secret)) {
+            exit('Secret error!');
+        }
+
+        ksort($param);
+        $outputStr = '';
+        foreach ($param as $k => &$v) {
+            if (empty($v)) {
+                exit('Param can\'t error!');
+            }
+            $outputStr .= $k . $v;
+        }
+
+        $outputStr = $secret . $outputStr . $body . $secret;
+        return strtoupper(md5($outputStr));
+    }
+
+    // 业务逻辑
+    public function request($baseUrl, $method, $body)
+    {
+        $this->param['method'] = $method;                                           // 此处填写要应对应BODY,具体参考白皮书的 ‘ERP调用的奇门API名称’
+        $this->param['timestamp'] = date("Y-m-d H:i:s");                               // 时间
+        $this->param['sign'] = $this->sign($this->secret, $this->param, $body);   // 利用body签名
+        $url = $baseUrl . "?" . http_build_query($this->param);
+        echo $url . "\n";
+        echo $body . "\n";
+        $return = $this->httpCurl($url, $body, 'post');
+
+        echo $return . "\n";
+
+    }
+
+    /**
+     * 请求数据
+     * @param $url             请求地址
+     * @param $data        提交数据
+     * @param $requestType  请求类型
+     */
+    public function httpCurl($url, $data, $requestType = 'get')
+    {
+        //初始化curl
+        $ch = curl_init();
+        //设置超时
+        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
+        curl_setopt($ch, CURLOPT_URL, $url);
+        curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
+        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
+        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
+        curl_setopt($ch, CURLOPT_HEADER, FALSE);
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
+        if (strtolower($requestType) == 'post') {
+            curl_setopt($ch, CURLOPT_POST, 1);
+            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
+        }
+        $return = curl_exec($ch);
+        curl_close($ch);
+        return $return;
+    }
+
+    public function json_to_xml($source, $charset = 'utf8')
+    {
+        if (empty($source)) {
+            return false;
+        }
+        //php5,以及以上,如果是更早版本,请查看JSON.php
+        $array = json_decode($source);
+        $xml = '';
+        $xml .= $this->change($array);
+        return $xml;
+    }
+
+    public function change($source)
+    {
+        $string = "";
+        foreach ($source as $k => $v) {
+            $string .= "<" . $k . ">";
+            //判断是否是数组,或者,对像
+            if (is_array($v) || is_object($v)) {
+                //是数组或者对像就的递归调用
+                $string .= $this->change($v);
+            } else {
+                //取得标签数据
+                $string .= $v;
+            }
+            $string .= "</" . $k . ">";
+        }
+        return $string;
+    }
+
+    public function xml_to_json($source)
+    {
+        if (is_file($source)) { //传的是文件,还是xml的string的判断
+            $xml_array = simplexml_load_file($source);
+        } else {
+            $xml_array = simplexml_load_string($source);
+        }
+        $json = json_encode($xml_array); //php5,以及以上,如果是更早版本,请查看JSON.php
+        return $json;
+    }
+}

+ 151 - 0
php/SingleitemSynchronizeRequest.php

@@ -0,0 +1,151 @@
+<?php
+/**
+ * TOP API: taobao.qimen.singleitem.synchronize request
+ * 
+ * @author auto create
+ * @since 1.0, 2017.05.12
+ */
+class SingleitemSynchronizeRequest
+{
+	/** 
+	 * 操作类型(两种类型:add|update)
+	 **/
+	public $actionType;
+	
+	/** 
+	 * 扩展属性
+	 **/
+    private $extendProps;
+	
+	/** 
+	 * 商品信息
+	 **/
+    public $item;
+	
+	/** 
+	 * 货主编码
+	 **/
+    public $ownerCode;
+	
+	/** 
+	 * 供应商编码
+	 **/
+    public $supplierCode;
+	
+	/** 
+	 * 供应商名称
+	 **/
+    public $supplierName;
+	
+	/** 
+	 * 仓库编码(统仓统配等无需ERP指定仓储编码的情况填OTHER)
+	 **/
+    public $warehouseCode;
+
+    private $apiParas = array();
+	
+	public function setActionType($actionType)
+	{
+		$this->actionType = $actionType;
+		$this->apiParas["actionType"] = $actionType;
+	}
+
+	public function getActionType()
+	{
+		return $this->actionType;
+	}
+
+	public function setExtendProps($extendProps)
+	{
+		$this->extendProps = $extendProps;
+		$this->apiParas["extendProps"] = $extendProps;
+	}
+
+	public function getExtendProps()
+	{
+		return $this->extendProps;
+	}
+
+	public function setItem($item)
+	{
+		$this->item = $item;
+		$this->apiParas["item"] = $item;
+	}
+
+	public function getItem()
+	{
+		return $this->item;
+	}
+
+	public function setOwnerCode($ownerCode)
+	{
+		$this->ownerCode = $ownerCode;
+		$this->apiParas["ownerCode"] = $ownerCode;
+	}
+
+	public function getOwnerCode()
+	{
+		return $this->ownerCode;
+	}
+
+	public function setSupplierCode($supplierCode)
+	{
+		$this->supplierCode = $supplierCode;
+		$this->apiParas["supplierCode"] = $supplierCode;
+	}
+
+	public function getSupplierCode()
+	{
+		return $this->supplierCode;
+	}
+
+	public function setSupplierName($supplierName)
+	{
+		$this->supplierName = $supplierName;
+		$this->apiParas["supplierName"] = $supplierName;
+	}
+
+	public function getSupplierName()
+	{
+		return $this->supplierName;
+	}
+
+	public function setWarehouseCode($warehouseCode)
+	{
+		$this->warehouseCode = $warehouseCode;
+		$this->apiParas["warehouseCode"] = $warehouseCode;
+	}
+
+	public function getWarehouseCode()
+	{
+		return $this->warehouseCode;
+	}
+
+	public function getApiMethodName()
+	{
+		return "singleitem.synchronize";
+	}
+	
+	public function getApiParas()
+	{
+		return $this->apiParas;
+	}
+	
+	public function check()
+	{
+		
+		RequestCheckUtil::checkNotNull($this->actionType,"actionType");
+		RequestCheckUtil::checkMaxLength($this->actionType,10,"actionType");
+		RequestCheckUtil::checkNotNull($this->ownerCode,"ownerCode");
+		RequestCheckUtil::checkMaxLength($this->ownerCode,50,"ownerCode");
+		RequestCheckUtil::checkMaxLength($this->supplierCode,50,"supplierCode");
+		RequestCheckUtil::checkMaxLength($this->supplierName,200,"supplierName");
+		RequestCheckUtil::checkNotNull($this->warehouseCode,"warehouseCode");
+		RequestCheckUtil::checkMaxLength($this->warehouseCode,50,"warehouseCode");
+	}
+	
+	public function putOtherTextParam($key, $value) {
+		$this->apiParas[$key] = $value;
+		$this->$key = $value;
+	}
+}

+ 33 - 0
php/test.php

@@ -0,0 +1,33 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: zhangtao
+ * Date: 2018/5/14
+ * Time: 下午3:49
+ */
+
+include "Qimenbody.php";
+include "Qimen.php";
+include "SingleitemSynchronizeRequest.php";
+
+$qimen = new Qimen();
+
+$itemDto = new SingleitemSynchronizeRequest;
+
+$itemDto->warehouseCode = "LTCK";
+$itemDto->putOtherTextParam("actionType", "ADD");
+$itemDto->putOtherTextParam("ownerCode", "lt");
+$itemDto->putOtherTextParam("supplierCode", "111");
+$itemDto->putOtherTextParam("supplierName", "111");
+$itemDto->item->itemCode = '000001';
+$itemDto->item->itemName = '000001';
+$itemDto->item->itemType = 'ZC';
+$itemDto->item->brandName = 'XXX品牌';
+$itemDto->item->barCode = '000001,111111';
+$jsonStr = json_encode($itemDto);
+echo $jsonStr . "\n";
+$body = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><request>' . $qimen->json_to_xml($jsonStr) . '</request>';
+
+$method = "singleitem.synchronize";
+
+$qimen->request("http://c-wms.iask.in:8081/BH_CLIS/qimen", "singleitem.synchronize", $body);