Przeglądaj źródła

添加集合单子

lvhao 3 dni temu
rodzic
commit
93d5e49d08
1 zmienionych plików z 64 dodań i 0 usunięć
  1. 64 0
      core/CoreApp/models/Model_usps.php

+ 64 - 0
core/CoreApp/models/Model_usps.php

@@ -1243,6 +1243,70 @@ function __construct(){
 			return $res;
 		}
 	}
+
+
+	/**
+	 * $trackingNumbers  快递单号列表
+	 */
+	public function createScanForm($trackingNumbers,$mailDate){
+		$accessToken = $this->get_token();
+		$url = "https://apis-tem.usps.com/scan-forms/v3/scan-form";
+		$fromAddress = [
+			"streetAddress"=>'819 6th Avenue Store front',
+			"city"=>'New York',
+			"state"=>"NY",
+			'ZIPCode'=>"10001",
+			"firstName"=>'Kenny',
+			'lastName'=>"Kwok",
+			'firm'=>'Long Human Hair Factory Inc',
+			'ignoreBadAddress'=>false
+		];
+		/**
+		 * 其中packages的数组格式
+		 * [
+		 * 	
+		 * ]
+		 */
+		$payload = [
+			"imageType"=>"PDF",
+			"labelType"=>"4X6LABEL",
+			"mailingDate" => $mailDate, // 格式 YYYY-MM-DD
+			//"overwriteMailingDate"=>false,
+			"entryFacilityZIPCode"=>"",//配送中心的邮编
+			"destinationEntryFacilityType"=>"",//设施类型
+			"packages" => $trackingNumbers,
+			"fromAddress" => $fromAddress, // 发货人地址,与标签上的fromAddress一致
+		];
+
+		$jsonPayload = json_encode($payload, JSON_UNESCAPED_SLASHES);
+
+		$ch = curl_init();
+		curl_setopt($ch, CURLOPT_URL, $url);
+		curl_setopt($ch, CURLOPT_POST, true);
+		curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonPayload);
+		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+		curl_setopt($ch, CURLOPT_HTTPHEADER, [
+			'Content-Type: application/json',
+			'Accept: application/vnd.usps.labels+json',
+			'X-Payment-Authorization-Token: ' . $accessToken,
+		]);
+
+		$response = curl_exec($ch);
+		$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+		curl_close($ch);
+
+		if ($httpCode == 200) {
+			// 响应可能也是multipart,包含PDF的SCAN Form
+			// 同样需要解析
+			echo "<pre>";
+			print_r($response);
+			die;
+			return $response;
+		} else {
+			throw new Exception('Failed to create SCAN form: ' . $response);
+		}
+
+	}