Наконец, мне удалось настроить XML-запрос отправки UPS, и я хотел бы поделиться им с другими людьми, борющимися с ним. Итак, вот полный PHP-объектно-ориентированный XML-код в соответствии с новейшей документацией с 2017 года.
Я делаю это, потому что в Интернете не так много полезной информации, и я хочу изменить это.
Этот код также решает многие распространенные ошибки, о которых вы можете найти здесь, в StackOverflow, но в основном без ответов. Это не вопрос, его учебник для тех, кто будет заниматься поставками UPS в будущем.
Мне удалось решить эти общие ошибки ИБП с моим кодом:
XML-документ плохо сформирован -> Эта ошибка была немного сложной, потому что я проверил мою структуру XML тысячу раз, и у меня было на 100% то же, что и в документации, но у меня была СТАРАЯ ДОКУМЕНТАЦИЯ, поэтому мало что изменилось, поэтому, когда я воссоздал структуру до самой новой, проблема была решена.
XML-документ хорошо сформирован, но документ недействителен -> Я действительно не знаю, что, черт возьми, вызывал эту ошибку, но изменение «логики отправки» немного разрешило его (вы можете взглянуть на конец структуры XML, как готово).
После того, как я решил эти две «самые большие» ошибки, ИБП-запросы начали работать, и появились правильные сообщения об ошибках, говорящие более подробно о том, что не так … например, «AccessLicenseNumber недействителен» и т. Д.
Поэтому первое, что вам нужно сделать, это зарегистрироваться на веб-сайте UPS и загрузить там самую новую документацию.
Примечание. Этот код отправляет первый запрос под названием «ConfirmRequest», который получит ответ «ConfirmResponse» и второй запрос под названием «AcceptRequest», который приводит к «AcceptResponse» в соответствии с изображением ниже!
Итак, вот полный скрипт, который создаст ваши структуры XML, отправит их в XML и даст вам ответы.
<?php // ACCESS: You will get these after you register on UPS Website $AccessLicenseNumber = 'xxxxxxx'; $UserID = 'xxxxxxx'; $Password = 'xxxxxxxxx'; // REQUEST ********************************* $CustomerContext = "something"; $RequestAction = "ShipConfirm"; // These values are contained in documentation $RequestOption = "nonvalidate"; ......... ......... ......... And many others ...
И вот структура XML! У меня есть красная вся документация, чтобы записать необходимую информацию обо всех элементах, чтобы вы могли легко отлаживать свой XML, когда вы вставляете, например, имя длиной более 30 символов
ВАЖНО : Возможно, вы заметили, что у меня есть только один заголовок, даже если в документации говорится, что он хочет ДВЕ HEADERS, это правда. Но я попытался отправить его только с одним, и он работает, поэтому не нужно иметь двух из них.
1. ОтгрузкаConfirmRequest
$domtree = new DOMDocument('1.0'); // <AccessRequest> $AccessRequest = $domtree->createElement("AccessRequest"); $AccessRequest->setAttribute("xml:lang", "en_US"); $domtree->appendChild($AccessRequest); // <AccessLicenseNumber> $AccessRequest->appendChild($domtree->createElement('AccessLicenseNumber', $AccessLicenseNumber)); // <UserId> $AccessRequest->appendChild($domtree->createElement('UserId', $UserID)); // <Password> $AccessRequest->appendChild($domtree->createElement('Password', $Password)); // </AccessRequest> // <ShipmentConfirmRequest> $ShipmentConfirmRequest = $domtree->createElement("ShipmentConfirmRequest"); $ShipmentConfirmRequest->setAttribute("xml:lang", "en_US"); $domtree->appendChild($ShipmentConfirmRequest); // <Request> $Request = $domtree->createElement("Request"); $ShipmentConfirmRequest->appendChild($Request); // <TransactionReference> $TransactionReference = $domtree->createElement("TransactionReference"); $Request->appendChild($TransactionReference); // <CustomerContext> $TransactionReference->appendChild($domtree->createElement('CustomerContext', $CustomerContext)); // Length: 1-512, Required: No // </TransactionReference> // <RequestAction> $Request->appendChild($domtree->createElement('RequestAction', $RequestAction)); // Length: 10, Required: Yes, Must be "ShipConfirm" // <RequestOption> $Request->appendChild($domtree->createElement('RequestOption', $RequestOption)); // Length: 1-256, Required: Yes, "validate" or "nonvalidate" // </Request> // <Shipment> $Shipment = $domtree->createElement("Shipment"); $ShipmentConfirmRequest->appendChild($Shipment); // <Shipper> $Shipper = $domtree->createElement("Shipper"); $Shipment->appendChild($Shipper); // <Name> $Shipper->appendChild($domtree->createElement('Name', $ShipperName)); // Length: 1-35, Required: Yes, Company Name // <AttentionName> $Shipper->appendChild($domtree->createElement('AttentionName', $ShipperAttentionName)); // Length: 1-35, Required: Cond, Required if destination is international // <PhoneNumber> $Shipper->appendChild($domtree->createElement('PhoneNumber', $ShipperPhoneNumber)); // Length: 1-15, Required: Cond // <ShipperNumber> $Shipper->appendChild($domtree->createElement('ShipperNumber', $ShipperNumber)); // Length: 6, Required: Yes // <Address> $Address = $domtree->createElement('Address'); $Shipper->appendChild($Address); // <AddressLine1> $Address->appendChild($domtree->createElement('AddressLine1', $ShipperAddressLine)); // Length: 1-35, Required: Yes // <City> $Address->appendChild($domtree->createElement('City', $ShipperCity)); // Length: 1-30, Required: Yes // <StateProvinceCode> $Address->appendChild($domtree->createElement('StateProvinceCode', $ShipperStateProvinceCode)); // Length: 2-5, Required: Cond, Required if shipper is in the US or CA. // <PostalCode> $Address->appendChild($domtree->createElement('PostalCode', $ShipperPostalCode)); // Length: 1-10, Required: Cond, For all other countries, the postal code is optional // <CountryCode> $Address->appendChild($domtree->createElement('CountryCode', $ShipperCountryCode)); // Length: 2, Required: Yes // </Address> // </Shipper> // <ShipTo> $ShipTo = $domtree->createElement("ShipTo"); $Shipment->appendChild($ShipTo); // <CompanyName> $ShipTo->appendChild($domtree->createElement('CompanyName', $ShipToCompanyName)); // Length: 1-35, Required: Yes // <AttentionName> $ShipTo->appendChild($domtree->createElement('AttentionName', $ShipToAttentionName)); // Length: 1-35, Required: Cond, for UPS Next Day Air Early service, and when ShipTo country is different than ShipFrom country. // <PhoneNumber> $ShipTo->appendChild($domtree->createElement('PhoneNumber', $ShipTo_phone_number)); // Length: 1-15, Required: Cond, Required for UPS Next Day Air Early service, and when Ship To country is different than the ShipFrom country. // <Address> $Address2 = $domtree->createElement('Address'); $ShipTo->appendChild($Address2); // <AddressLine1> $Address2->appendChild($domtree->createElement('AddressLine1', $ShipToAddressLine)); // Length: 1-35, Required: Yes // <City> $Address2->appendChild($domtree->createElement('City', $ShipToCity)); // Length: 1-30, Required: Yes // <StateProvinceCode> $Address2->appendChild($domtree->createElement('StateProvinceCode', $ShipToStateProvinceCode)); // Length: 2-5, Required: Cond, Required if shipper is in the US or CA. // <PostalCode> $Address2->appendChild($domtree->createElement('PostalCode', $ShipToPostalCode)); // Length: 1-10, Required: Cond, For all other countries, the postal code is optional // <CountryCode> $Address2->appendChild($domtree->createElement('CountryCode', $ShipToCountryCode)); // Length: 2, Required: Yes // </Address> // </ShipTo> // <PaymentInformation> $PaymentInformation = $domtree->createElement("PaymentInformation"); $Shipment->AppendChild($PaymentInformation); // <Prepaid> $Prepaid = $domtree->createElement("Prepaid"); $PaymentInformation->appendChild($Prepaid); // <BillShipper> $BillShipper = $domtree->createElement("BillShipper"); $Prepaid->appendChild($BillShipper); // <AccountNumber> $BillShipper->appendChild($domtree->createElement('AccountNumber', $AccountNumber)); // Length: 6, Required: Cond, Based on PaymentInformation container, Must be the same UPS account number as the one provided in Shipper/ShipperNumber. // </BillShipper> // </Prepaid> // </PaymentInformation> // <Service> $Service = $domtree->createElement("Service"); $Shipment->appendChild($Service); // <Code> $Service->appendChild($domtree->createElement('Code', $ServiceCode)); // Length: 2, Required: Yes, 01 = Next Day Air 02 = 2nd Day Air ... // </Service> Here is a for loop which creates as many <Package> elements as you want (You can remove this if you want to send only one PACKAGE) for ($i = 0; $i < sizeof($Pack_IDs); $i++) { // <Package> $Package = $domtree->createElement('Package'); $Shipment->appendChild($Package); // <PackagingType> $PackagingType = $domtree->createElement('PackagingType'); $Package->appendChild($PackagingType); // <Code> $PackagingType->appendChild($domtree->createElement('Code', $PackageTypeCode)); // Length: 2, Required: Yes, 01 = UPS Letter 02 = Customer Supplied Package ... // </PackagingType> // <Description> $Package->appendChild($domtree->createElement('Description', $Description)); // Length: 1-35, Required: Cond, Required for shipment with return service. // </Description> // <Dimensions> $Dimensions = $domtree->createElement('Dimensions'); // Required: Cond, Length + 2*(Width + Height) must be less than or equal to 130 IN or 330 CM. $Package->appendChild($Dimensions); // <UnitOfMeasurement> $UnitOfMeasurement = $domtree->createElement('UnitOfMeasurement'); $Dimensions->appendChild($UnitOfMeasurement); // <Code> $UnitOfMeasurement->appendChild($domtree->createElement('Code', $DimensionUnitOfMeasurementCode)); // Length: 2, Required: Yes*, Codes are: IN = Inches, CM = Centimeters, 00 = Metric Units Of Measurement, 01 = English Units of Measurement. // </UnitOfMeasurement> // <Length> $Dimensions->appendChild($domtree->createElement('Length', $PackageLength)); // Length: 9, Required: Yes*, Valid values are 0 to 108 IN and 0 to 270 CM. // <Width> $Dimensions->appendChild($domtree->createElement('Width', $PackageWidth)); // Length: 9, Required: Yes* // <Height> $Dimensions->appendChild($domtree->createElement('Height', $PackageHeight)); // Length: 9, Required: Yes* // </Dimensions> // <PackageWeight> $PackageWeight = $domtree->createElement('PackageWeight'); $Package->appendChild($PackageWeight); // <UnitOfMeasurement> $UnitOfMeasurement2 = $domtree->createElement('UnitOfMeasurement'); $PackageWeight->appendChild($UnitOfMeasurement2); // <Code> $UnitOfMeasurement2->appendChild($domtree->createElement('Code', $WeightUnitOfMeasurementCode)); // Length: 3, Required: Cond, LBS = Pounds KGS = Kilograms OZS = Ounces ... // <Weight> $PackageWeight->appendChild($domtree->createElement('Weight', $Pack_weights[$i])); // Length: 1-5, Required: Yes*, Weight accepted for letters/envelopes. // </UnitOfMeasurement> // </PackageWeight> // </Package> } // </Shipment> // <LabelSpecification> $LabelSpecification = $domtree->createElement('LabelSpecification'); $ShipmentConfirmRequest->appendChild($LabelSpecification); // <LabelPrintMethod> $LabelPrintMethod = $domtree->createElement('LabelPrintMethod'); $LabelSpecification->appendChild($LabelPrintMethod); // <Code> $LabelPrintMethod->appendChild($domtree->createElement('Code', $LabelCode)); // Length: 4, Required: Yes* // </LabelPrintMethod> // <LabelImageFormat> $LabelImageFormat = $domtree->createElement('LabelImageFormat'); $LabelSpecification->appendChild($LabelImageFormat); // <Code> $LabelImageFormat->appendChild($domtree->createElement('Code', $LabelImageCode)); // Length: 3, Required: Cond, Required if ShipmentConfirmRequest/LabelSpecification/LabelPrintMethod/Code = GIF. Valid values are GIF or PNG. Only GIF is supported on the remote server. // </LabelImageFormat> // </LabelSpecification> // </ShipmentConfimRequest>
-$domtree = new DOMDocument('1.0'); // <AccessRequest> $AccessRequest = $domtree->createElement("AccessRequest"); $AccessRequest->setAttribute("xml:lang", "en_US"); $domtree->appendChild($AccessRequest); // <AccessLicenseNumber> $AccessRequest->appendChild($domtree->createElement('AccessLicenseNumber', $AccessLicenseNumber)); // <UserId> $AccessRequest->appendChild($domtree->createElement('UserId', $UserID)); // <Password> $AccessRequest->appendChild($domtree->createElement('Password', $Password)); // </AccessRequest> // <ShipmentConfirmRequest> $ShipmentConfirmRequest = $domtree->createElement("ShipmentConfirmRequest"); $ShipmentConfirmRequest->setAttribute("xml:lang", "en_US"); $domtree->appendChild($ShipmentConfirmRequest); // <Request> $Request = $domtree->createElement("Request"); $ShipmentConfirmRequest->appendChild($Request); // <TransactionReference> $TransactionReference = $domtree->createElement("TransactionReference"); $Request->appendChild($TransactionReference); // <CustomerContext> $TransactionReference->appendChild($domtree->createElement('CustomerContext', $CustomerContext)); // Length: 1-512, Required: No // </TransactionReference> // <RequestAction> $Request->appendChild($domtree->createElement('RequestAction', $RequestAction)); // Length: 10, Required: Yes, Must be "ShipConfirm" // <RequestOption> $Request->appendChild($domtree->createElement('RequestOption', $RequestOption)); // Length: 1-256, Required: Yes, "validate" or "nonvalidate" // </Request> // <Shipment> $Shipment = $domtree->createElement("Shipment"); $ShipmentConfirmRequest->appendChild($Shipment); // <Shipper> $Shipper = $domtree->createElement("Shipper"); $Shipment->appendChild($Shipper); // <Name> $Shipper->appendChild($domtree->createElement('Name', $ShipperName)); // Length: 1-35, Required: Yes, Company Name // <AttentionName> $Shipper->appendChild($domtree->createElement('AttentionName', $ShipperAttentionName)); // Length: 1-35, Required: Cond, Required if destination is international // <PhoneNumber> $Shipper->appendChild($domtree->createElement('PhoneNumber', $ShipperPhoneNumber)); // Length: 1-15, Required: Cond // <ShipperNumber> $Shipper->appendChild($domtree->createElement('ShipperNumber', $ShipperNumber)); // Length: 6, Required: Yes // <Address> $Address = $domtree->createElement('Address'); $Shipper->appendChild($Address); // <AddressLine1> $Address->appendChild($domtree->createElement('AddressLine1', $ShipperAddressLine)); // Length: 1-35, Required: Yes // <City> $Address->appendChild($domtree->createElement('City', $ShipperCity)); // Length: 1-30, Required: Yes // <StateProvinceCode> $Address->appendChild($domtree->createElement('StateProvinceCode', $ShipperStateProvinceCode)); // Length: 2-5, Required: Cond, Required if shipper is in the US or CA. // <PostalCode> $Address->appendChild($domtree->createElement('PostalCode', $ShipperPostalCode)); // Length: 1-10, Required: Cond, For all other countries, the postal code is optional // <CountryCode> $Address->appendChild($domtree->createElement('CountryCode', $ShipperCountryCode)); // Length: 2, Required: Yes // </Address> // </Shipper> // <ShipTo> $ShipTo = $domtree->createElement("ShipTo"); $Shipment->appendChild($ShipTo); // <CompanyName> $ShipTo->appendChild($domtree->createElement('CompanyName', $ShipToCompanyName)); // Length: 1-35, Required: Yes // <AttentionName> $ShipTo->appendChild($domtree->createElement('AttentionName', $ShipToAttentionName)); // Length: 1-35, Required: Cond, for UPS Next Day Air Early service, and when ShipTo country is different than ShipFrom country. // <PhoneNumber> $ShipTo->appendChild($domtree->createElement('PhoneNumber', $ShipTo_phone_number)); // Length: 1-15, Required: Cond, Required for UPS Next Day Air Early service, and when Ship To country is different than the ShipFrom country. // <Address> $Address2 = $domtree->createElement('Address'); $ShipTo->appendChild($Address2); // <AddressLine1> $Address2->appendChild($domtree->createElement('AddressLine1', $ShipToAddressLine)); // Length: 1-35, Required: Yes // <City> $Address2->appendChild($domtree->createElement('City', $ShipToCity)); // Length: 1-30, Required: Yes // <StateProvinceCode> $Address2->appendChild($domtree->createElement('StateProvinceCode', $ShipToStateProvinceCode)); // Length: 2-5, Required: Cond, Required if shipper is in the US or CA. // <PostalCode> $Address2->appendChild($domtree->createElement('PostalCode', $ShipToPostalCode)); // Length: 1-10, Required: Cond, For all other countries, the postal code is optional // <CountryCode> $Address2->appendChild($domtree->createElement('CountryCode', $ShipToCountryCode)); // Length: 2, Required: Yes // </Address> // </ShipTo> // <PaymentInformation> $PaymentInformation = $domtree->createElement("PaymentInformation"); $Shipment->AppendChild($PaymentInformation); // <Prepaid> $Prepaid = $domtree->createElement("Prepaid"); $PaymentInformation->appendChild($Prepaid); // <BillShipper> $BillShipper = $domtree->createElement("BillShipper"); $Prepaid->appendChild($BillShipper); // <AccountNumber> $BillShipper->appendChild($domtree->createElement('AccountNumber', $AccountNumber)); // Length: 6, Required: Cond, Based on PaymentInformation container, Must be the same UPS account number as the one provided in Shipper/ShipperNumber. // </BillShipper> // </Prepaid> // </PaymentInformation> // <Service> $Service = $domtree->createElement("Service"); $Shipment->appendChild($Service); // <Code> $Service->appendChild($domtree->createElement('Code', $ServiceCode)); // Length: 2, Required: Yes, 01 = Next Day Air 02 = 2nd Day Air ... // </Service> Here is a for loop which creates as many <Package> elements as you want (You can remove this if you want to send only one PACKAGE) for ($i = 0; $i < sizeof($Pack_IDs); $i++) { // <Package> $Package = $domtree->createElement('Package'); $Shipment->appendChild($Package); // <PackagingType> $PackagingType = $domtree->createElement('PackagingType'); $Package->appendChild($PackagingType); // <Code> $PackagingType->appendChild($domtree->createElement('Code', $PackageTypeCode)); // Length: 2, Required: Yes, 01 = UPS Letter 02 = Customer Supplied Package ... // </PackagingType> // <Description> $Package->appendChild($domtree->createElement('Description', $Description)); // Length: 1-35, Required: Cond, Required for shipment with return service. // </Description> // <Dimensions> $Dimensions = $domtree->createElement('Dimensions'); // Required: Cond, Length + 2*(Width + Height) must be less than or equal to 130 IN or 330 CM. $Package->appendChild($Dimensions); // <UnitOfMeasurement> $UnitOfMeasurement = $domtree->createElement('UnitOfMeasurement'); $Dimensions->appendChild($UnitOfMeasurement); // <Code> $UnitOfMeasurement->appendChild($domtree->createElement('Code', $DimensionUnitOfMeasurementCode)); // Length: 2, Required: Yes*, Codes are: IN = Inches, CM = Centimeters, 00 = Metric Units Of Measurement, 01 = English Units of Measurement. // </UnitOfMeasurement> // <Length> $Dimensions->appendChild($domtree->createElement('Length', $PackageLength)); // Length: 9, Required: Yes*, Valid values are 0 to 108 IN and 0 to 270 CM. // <Width> $Dimensions->appendChild($domtree->createElement('Width', $PackageWidth)); // Length: 9, Required: Yes* // <Height> $Dimensions->appendChild($domtree->createElement('Height', $PackageHeight)); // Length: 9, Required: Yes* // </Dimensions> // <PackageWeight> $PackageWeight = $domtree->createElement('PackageWeight'); $Package->appendChild($PackageWeight); // <UnitOfMeasurement> $UnitOfMeasurement2 = $domtree->createElement('UnitOfMeasurement'); $PackageWeight->appendChild($UnitOfMeasurement2); // <Code> $UnitOfMeasurement2->appendChild($domtree->createElement('Code', $WeightUnitOfMeasurementCode)); // Length: 3, Required: Cond, LBS = Pounds KGS = Kilograms OZS = Ounces ... // <Weight> $PackageWeight->appendChild($domtree->createElement('Weight', $Pack_weights[$i])); // Length: 1-5, Required: Yes*, Weight accepted for letters/envelopes. // </UnitOfMeasurement> // </PackageWeight> // </Package> } // </Shipment> // <LabelSpecification> $LabelSpecification = $domtree->createElement('LabelSpecification'); $ShipmentConfirmRequest->appendChild($LabelSpecification); // <LabelPrintMethod> $LabelPrintMethod = $domtree->createElement('LabelPrintMethod'); $LabelSpecification->appendChild($LabelPrintMethod); // <Code> $LabelPrintMethod->appendChild($domtree->createElement('Code', $LabelCode)); // Length: 4, Required: Yes* // </LabelPrintMethod> // <LabelImageFormat> $LabelImageFormat = $domtree->createElement('LabelImageFormat'); $LabelSpecification->appendChild($LabelImageFormat); // <Code> $LabelImageFormat->appendChild($domtree->createElement('Code', $LabelImageCode)); // Length: 3, Required: Cond, Required if ShipmentConfirmRequest/LabelSpecification/LabelPrintMethod/Code = GIF. Valid values are GIF or PNG. Only GIF is supported on the remote server. // </LabelImageFormat> // </LabelSpecification> // </ShipmentConfimRequest>
-$domtree = new DOMDocument('1.0'); // <AccessRequest> $AccessRequest = $domtree->createElement("AccessRequest"); $AccessRequest->setAttribute("xml:lang", "en_US"); $domtree->appendChild($AccessRequest); // <AccessLicenseNumber> $AccessRequest->appendChild($domtree->createElement('AccessLicenseNumber', $AccessLicenseNumber)); // <UserId> $AccessRequest->appendChild($domtree->createElement('UserId', $UserID)); // <Password> $AccessRequest->appendChild($domtree->createElement('Password', $Password)); // </AccessRequest> // <ShipmentConfirmRequest> $ShipmentConfirmRequest = $domtree->createElement("ShipmentConfirmRequest"); $ShipmentConfirmRequest->setAttribute("xml:lang", "en_US"); $domtree->appendChild($ShipmentConfirmRequest); // <Request> $Request = $domtree->createElement("Request"); $ShipmentConfirmRequest->appendChild($Request); // <TransactionReference> $TransactionReference = $domtree->createElement("TransactionReference"); $Request->appendChild($TransactionReference); // <CustomerContext> $TransactionReference->appendChild($domtree->createElement('CustomerContext', $CustomerContext)); // Length: 1-512, Required: No // </TransactionReference> // <RequestAction> $Request->appendChild($domtree->createElement('RequestAction', $RequestAction)); // Length: 10, Required: Yes, Must be "ShipConfirm" // <RequestOption> $Request->appendChild($domtree->createElement('RequestOption', $RequestOption)); // Length: 1-256, Required: Yes, "validate" or "nonvalidate" // </Request> // <Shipment> $Shipment = $domtree->createElement("Shipment"); $ShipmentConfirmRequest->appendChild($Shipment); // <Shipper> $Shipper = $domtree->createElement("Shipper"); $Shipment->appendChild($Shipper); // <Name> $Shipper->appendChild($domtree->createElement('Name', $ShipperName)); // Length: 1-35, Required: Yes, Company Name // <AttentionName> $Shipper->appendChild($domtree->createElement('AttentionName', $ShipperAttentionName)); // Length: 1-35, Required: Cond, Required if destination is international // <PhoneNumber> $Shipper->appendChild($domtree->createElement('PhoneNumber', $ShipperPhoneNumber)); // Length: 1-15, Required: Cond // <ShipperNumber> $Shipper->appendChild($domtree->createElement('ShipperNumber', $ShipperNumber)); // Length: 6, Required: Yes // <Address> $Address = $domtree->createElement('Address'); $Shipper->appendChild($Address); // <AddressLine1> $Address->appendChild($domtree->createElement('AddressLine1', $ShipperAddressLine)); // Length: 1-35, Required: Yes // <City> $Address->appendChild($domtree->createElement('City', $ShipperCity)); // Length: 1-30, Required: Yes // <StateProvinceCode> $Address->appendChild($domtree->createElement('StateProvinceCode', $ShipperStateProvinceCode)); // Length: 2-5, Required: Cond, Required if shipper is in the US or CA. // <PostalCode> $Address->appendChild($domtree->createElement('PostalCode', $ShipperPostalCode)); // Length: 1-10, Required: Cond, For all other countries, the postal code is optional // <CountryCode> $Address->appendChild($domtree->createElement('CountryCode', $ShipperCountryCode)); // Length: 2, Required: Yes // </Address> // </Shipper> // <ShipTo> $ShipTo = $domtree->createElement("ShipTo"); $Shipment->appendChild($ShipTo); // <CompanyName> $ShipTo->appendChild($domtree->createElement('CompanyName', $ShipToCompanyName)); // Length: 1-35, Required: Yes // <AttentionName> $ShipTo->appendChild($domtree->createElement('AttentionName', $ShipToAttentionName)); // Length: 1-35, Required: Cond, for UPS Next Day Air Early service, and when ShipTo country is different than ShipFrom country. // <PhoneNumber> $ShipTo->appendChild($domtree->createElement('PhoneNumber', $ShipTo_phone_number)); // Length: 1-15, Required: Cond, Required for UPS Next Day Air Early service, and when Ship To country is different than the ShipFrom country. // <Address> $Address2 = $domtree->createElement('Address'); $ShipTo->appendChild($Address2); // <AddressLine1> $Address2->appendChild($domtree->createElement('AddressLine1', $ShipToAddressLine)); // Length: 1-35, Required: Yes // <City> $Address2->appendChild($domtree->createElement('City', $ShipToCity)); // Length: 1-30, Required: Yes // <StateProvinceCode> $Address2->appendChild($domtree->createElement('StateProvinceCode', $ShipToStateProvinceCode)); // Length: 2-5, Required: Cond, Required if shipper is in the US or CA. // <PostalCode> $Address2->appendChild($domtree->createElement('PostalCode', $ShipToPostalCode)); // Length: 1-10, Required: Cond, For all other countries, the postal code is optional // <CountryCode> $Address2->appendChild($domtree->createElement('CountryCode', $ShipToCountryCode)); // Length: 2, Required: Yes // </Address> // </ShipTo> // <PaymentInformation> $PaymentInformation = $domtree->createElement("PaymentInformation"); $Shipment->AppendChild($PaymentInformation); // <Prepaid> $Prepaid = $domtree->createElement("Prepaid"); $PaymentInformation->appendChild($Prepaid); // <BillShipper> $BillShipper = $domtree->createElement("BillShipper"); $Prepaid->appendChild($BillShipper); // <AccountNumber> $BillShipper->appendChild($domtree->createElement('AccountNumber', $AccountNumber)); // Length: 6, Required: Cond, Based on PaymentInformation container, Must be the same UPS account number as the one provided in Shipper/ShipperNumber. // </BillShipper> // </Prepaid> // </PaymentInformation> // <Service> $Service = $domtree->createElement("Service"); $Shipment->appendChild($Service); // <Code> $Service->appendChild($domtree->createElement('Code', $ServiceCode)); // Length: 2, Required: Yes, 01 = Next Day Air 02 = 2nd Day Air ... // </Service> Here is a for loop which creates as many <Package> elements as you want (You can remove this if you want to send only one PACKAGE) for ($i = 0; $i < sizeof($Pack_IDs); $i++) { // <Package> $Package = $domtree->createElement('Package'); $Shipment->appendChild($Package); // <PackagingType> $PackagingType = $domtree->createElement('PackagingType'); $Package->appendChild($PackagingType); // <Code> $PackagingType->appendChild($domtree->createElement('Code', $PackageTypeCode)); // Length: 2, Required: Yes, 01 = UPS Letter 02 = Customer Supplied Package ... // </PackagingType> // <Description> $Package->appendChild($domtree->createElement('Description', $Description)); // Length: 1-35, Required: Cond, Required for shipment with return service. // </Description> // <Dimensions> $Dimensions = $domtree->createElement('Dimensions'); // Required: Cond, Length + 2*(Width + Height) must be less than or equal to 130 IN or 330 CM. $Package->appendChild($Dimensions); // <UnitOfMeasurement> $UnitOfMeasurement = $domtree->createElement('UnitOfMeasurement'); $Dimensions->appendChild($UnitOfMeasurement); // <Code> $UnitOfMeasurement->appendChild($domtree->createElement('Code', $DimensionUnitOfMeasurementCode)); // Length: 2, Required: Yes*, Codes are: IN = Inches, CM = Centimeters, 00 = Metric Units Of Measurement, 01 = English Units of Measurement. // </UnitOfMeasurement> // <Length> $Dimensions->appendChild($domtree->createElement('Length', $PackageLength)); // Length: 9, Required: Yes*, Valid values are 0 to 108 IN and 0 to 270 CM. // <Width> $Dimensions->appendChild($domtree->createElement('Width', $PackageWidth)); // Length: 9, Required: Yes* // <Height> $Dimensions->appendChild($domtree->createElement('Height', $PackageHeight)); // Length: 9, Required: Yes* // </Dimensions> // <PackageWeight> $PackageWeight = $domtree->createElement('PackageWeight'); $Package->appendChild($PackageWeight); // <UnitOfMeasurement> $UnitOfMeasurement2 = $domtree->createElement('UnitOfMeasurement'); $PackageWeight->appendChild($UnitOfMeasurement2); // <Code> $UnitOfMeasurement2->appendChild($domtree->createElement('Code', $WeightUnitOfMeasurementCode)); // Length: 3, Required: Cond, LBS = Pounds KGS = Kilograms OZS = Ounces ... // <Weight> $PackageWeight->appendChild($domtree->createElement('Weight', $Pack_weights[$i])); // Length: 1-5, Required: Yes*, Weight accepted for letters/envelopes. // </UnitOfMeasurement> // </PackageWeight> // </Package> } // </Shipment> // <LabelSpecification> $LabelSpecification = $domtree->createElement('LabelSpecification'); $ShipmentConfirmRequest->appendChild($LabelSpecification); // <LabelPrintMethod> $LabelPrintMethod = $domtree->createElement('LabelPrintMethod'); $LabelSpecification->appendChild($LabelPrintMethod); // <Code> $LabelPrintMethod->appendChild($domtree->createElement('Code', $LabelCode)); // Length: 4, Required: Yes* // </LabelPrintMethod> // <LabelImageFormat> $LabelImageFormat = $domtree->createElement('LabelImageFormat'); $LabelSpecification->appendChild($LabelImageFormat); // <Code> $LabelImageFormat->appendChild($domtree->createElement('Code', $LabelImageCode)); // Length: 3, Required: Cond, Required if ShipmentConfirmRequest/LabelSpecification/LabelPrintMethod/Code = GIF. Valid values are GIF or PNG. Only GIF is supported on the remote server. // </LabelImageFormat> // </LabelSpecification> // </ShipmentConfimRequest>
-$domtree = new DOMDocument('1.0'); // <AccessRequest> $AccessRequest = $domtree->createElement("AccessRequest"); $AccessRequest->setAttribute("xml:lang", "en_US"); $domtree->appendChild($AccessRequest); // <AccessLicenseNumber> $AccessRequest->appendChild($domtree->createElement('AccessLicenseNumber', $AccessLicenseNumber)); // <UserId> $AccessRequest->appendChild($domtree->createElement('UserId', $UserID)); // <Password> $AccessRequest->appendChild($domtree->createElement('Password', $Password)); // </AccessRequest> // <ShipmentConfirmRequest> $ShipmentConfirmRequest = $domtree->createElement("ShipmentConfirmRequest"); $ShipmentConfirmRequest->setAttribute("xml:lang", "en_US"); $domtree->appendChild($ShipmentConfirmRequest); // <Request> $Request = $domtree->createElement("Request"); $ShipmentConfirmRequest->appendChild($Request); // <TransactionReference> $TransactionReference = $domtree->createElement("TransactionReference"); $Request->appendChild($TransactionReference); // <CustomerContext> $TransactionReference->appendChild($domtree->createElement('CustomerContext', $CustomerContext)); // Length: 1-512, Required: No // </TransactionReference> // <RequestAction> $Request->appendChild($domtree->createElement('RequestAction', $RequestAction)); // Length: 10, Required: Yes, Must be "ShipConfirm" // <RequestOption> $Request->appendChild($domtree->createElement('RequestOption', $RequestOption)); // Length: 1-256, Required: Yes, "validate" or "nonvalidate" // </Request> // <Shipment> $Shipment = $domtree->createElement("Shipment"); $ShipmentConfirmRequest->appendChild($Shipment); // <Shipper> $Shipper = $domtree->createElement("Shipper"); $Shipment->appendChild($Shipper); // <Name> $Shipper->appendChild($domtree->createElement('Name', $ShipperName)); // Length: 1-35, Required: Yes, Company Name // <AttentionName> $Shipper->appendChild($domtree->createElement('AttentionName', $ShipperAttentionName)); // Length: 1-35, Required: Cond, Required if destination is international // <PhoneNumber> $Shipper->appendChild($domtree->createElement('PhoneNumber', $ShipperPhoneNumber)); // Length: 1-15, Required: Cond // <ShipperNumber> $Shipper->appendChild($domtree->createElement('ShipperNumber', $ShipperNumber)); // Length: 6, Required: Yes // <Address> $Address = $domtree->createElement('Address'); $Shipper->appendChild($Address); // <AddressLine1> $Address->appendChild($domtree->createElement('AddressLine1', $ShipperAddressLine)); // Length: 1-35, Required: Yes // <City> $Address->appendChild($domtree->createElement('City', $ShipperCity)); // Length: 1-30, Required: Yes // <StateProvinceCode> $Address->appendChild($domtree->createElement('StateProvinceCode', $ShipperStateProvinceCode)); // Length: 2-5, Required: Cond, Required if shipper is in the US or CA. // <PostalCode> $Address->appendChild($domtree->createElement('PostalCode', $ShipperPostalCode)); // Length: 1-10, Required: Cond, For all other countries, the postal code is optional // <CountryCode> $Address->appendChild($domtree->createElement('CountryCode', $ShipperCountryCode)); // Length: 2, Required: Yes // </Address> // </Shipper> // <ShipTo> $ShipTo = $domtree->createElement("ShipTo"); $Shipment->appendChild($ShipTo); // <CompanyName> $ShipTo->appendChild($domtree->createElement('CompanyName', $ShipToCompanyName)); // Length: 1-35, Required: Yes // <AttentionName> $ShipTo->appendChild($domtree->createElement('AttentionName', $ShipToAttentionName)); // Length: 1-35, Required: Cond, for UPS Next Day Air Early service, and when ShipTo country is different than ShipFrom country. // <PhoneNumber> $ShipTo->appendChild($domtree->createElement('PhoneNumber', $ShipTo_phone_number)); // Length: 1-15, Required: Cond, Required for UPS Next Day Air Early service, and when Ship To country is different than the ShipFrom country. // <Address> $Address2 = $domtree->createElement('Address'); $ShipTo->appendChild($Address2); // <AddressLine1> $Address2->appendChild($domtree->createElement('AddressLine1', $ShipToAddressLine)); // Length: 1-35, Required: Yes // <City> $Address2->appendChild($domtree->createElement('City', $ShipToCity)); // Length: 1-30, Required: Yes // <StateProvinceCode> $Address2->appendChild($domtree->createElement('StateProvinceCode', $ShipToStateProvinceCode)); // Length: 2-5, Required: Cond, Required if shipper is in the US or CA. // <PostalCode> $Address2->appendChild($domtree->createElement('PostalCode', $ShipToPostalCode)); // Length: 1-10, Required: Cond, For all other countries, the postal code is optional // <CountryCode> $Address2->appendChild($domtree->createElement('CountryCode', $ShipToCountryCode)); // Length: 2, Required: Yes // </Address> // </ShipTo> // <PaymentInformation> $PaymentInformation = $domtree->createElement("PaymentInformation"); $Shipment->AppendChild($PaymentInformation); // <Prepaid> $Prepaid = $domtree->createElement("Prepaid"); $PaymentInformation->appendChild($Prepaid); // <BillShipper> $BillShipper = $domtree->createElement("BillShipper"); $Prepaid->appendChild($BillShipper); // <AccountNumber> $BillShipper->appendChild($domtree->createElement('AccountNumber', $AccountNumber)); // Length: 6, Required: Cond, Based on PaymentInformation container, Must be the same UPS account number as the one provided in Shipper/ShipperNumber. // </BillShipper> // </Prepaid> // </PaymentInformation> // <Service> $Service = $domtree->createElement("Service"); $Shipment->appendChild($Service); // <Code> $Service->appendChild($domtree->createElement('Code', $ServiceCode)); // Length: 2, Required: Yes, 01 = Next Day Air 02 = 2nd Day Air ... // </Service> Here is a for loop which creates as many <Package> elements as you want (You can remove this if you want to send only one PACKAGE) for ($i = 0; $i < sizeof($Pack_IDs); $i++) { // <Package> $Package = $domtree->createElement('Package'); $Shipment->appendChild($Package); // <PackagingType> $PackagingType = $domtree->createElement('PackagingType'); $Package->appendChild($PackagingType); // <Code> $PackagingType->appendChild($domtree->createElement('Code', $PackageTypeCode)); // Length: 2, Required: Yes, 01 = UPS Letter 02 = Customer Supplied Package ... // </PackagingType> // <Description> $Package->appendChild($domtree->createElement('Description', $Description)); // Length: 1-35, Required: Cond, Required for shipment with return service. // </Description> // <Dimensions> $Dimensions = $domtree->createElement('Dimensions'); // Required: Cond, Length + 2*(Width + Height) must be less than or equal to 130 IN or 330 CM. $Package->appendChild($Dimensions); // <UnitOfMeasurement> $UnitOfMeasurement = $domtree->createElement('UnitOfMeasurement'); $Dimensions->appendChild($UnitOfMeasurement); // <Code> $UnitOfMeasurement->appendChild($domtree->createElement('Code', $DimensionUnitOfMeasurementCode)); // Length: 2, Required: Yes*, Codes are: IN = Inches, CM = Centimeters, 00 = Metric Units Of Measurement, 01 = English Units of Measurement. // </UnitOfMeasurement> // <Length> $Dimensions->appendChild($domtree->createElement('Length', $PackageLength)); // Length: 9, Required: Yes*, Valid values are 0 to 108 IN and 0 to 270 CM. // <Width> $Dimensions->appendChild($domtree->createElement('Width', $PackageWidth)); // Length: 9, Required: Yes* // <Height> $Dimensions->appendChild($domtree->createElement('Height', $PackageHeight)); // Length: 9, Required: Yes* // </Dimensions> // <PackageWeight> $PackageWeight = $domtree->createElement('PackageWeight'); $Package->appendChild($PackageWeight); // <UnitOfMeasurement> $UnitOfMeasurement2 = $domtree->createElement('UnitOfMeasurement'); $PackageWeight->appendChild($UnitOfMeasurement2); // <Code> $UnitOfMeasurement2->appendChild($domtree->createElement('Code', $WeightUnitOfMeasurementCode)); // Length: 3, Required: Cond, LBS = Pounds KGS = Kilograms OZS = Ounces ... // <Weight> $PackageWeight->appendChild($domtree->createElement('Weight', $Pack_weights[$i])); // Length: 1-5, Required: Yes*, Weight accepted for letters/envelopes. // </UnitOfMeasurement> // </PackageWeight> // </Package> } // </Shipment> // <LabelSpecification> $LabelSpecification = $domtree->createElement('LabelSpecification'); $ShipmentConfirmRequest->appendChild($LabelSpecification); // <LabelPrintMethod> $LabelPrintMethod = $domtree->createElement('LabelPrintMethod'); $LabelSpecification->appendChild($LabelPrintMethod); // <Code> $LabelPrintMethod->appendChild($domtree->createElement('Code', $LabelCode)); // Length: 4, Required: Yes* // </LabelPrintMethod> // <LabelImageFormat> $LabelImageFormat = $domtree->createElement('LabelImageFormat'); $LabelSpecification->appendChild($LabelImageFormat); // <Code> $LabelImageFormat->appendChild($domtree->createElement('Code', $LabelImageCode)); // Length: 3, Required: Cond, Required if ShipmentConfirmRequest/LabelSpecification/LabelPrintMethod/Code = GIF. Valid values are GIF or PNG. Only GIF is supported on the remote server. // </LabelImageFormat> // </LabelSpecification> // </ShipmentConfimRequest>
Итак, теперь мы подготовили и заполнили XML-структуру. Я рекомендую вам напечатать структуру XML и тщательно сравнить ее с документацией несколько раз, чтобы избежать многих ошибок только потому, что вы добавили что-то к неправильному родителю или что-то еще.
И вот код, который вам нужен, чтобы отправить XML и получить запрос.
2. ShipmentConfirmResponse
$domtree->preserveWhiteSpace = true; $domtree->formatOutput = true; $xml_string = $domtree->saveXML(); // UPS Address $url = 'https://wwwcie.ups.com/ups.app/xml/ShipConfirm'; // IMPORTANT: This is a testing URL address, dont be scared to send your request (Real URL is different -> documentation) // SEND THE REQUEST $stream_options = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => "$xml_string", ), ); $context = stream_context_create($stream_options); $response = file_get_contents($url, null, $context); // Response XML structure
Теперь у вас есть структура XML ответа, хранящаяся в переменной $ response, чтобы вы могли получить доступ к любым данным, которые вы хотите от нее
// Response handling $ShipmentConfirmResponse = new SimpleXMLElement($response); if ((string)$ShipmentConfirmResponse->Response->ResponseStatusCode == 1) { // If the response is "success" then continue with second request // If ShipmentCofirmRequest is successful, send ShipmentAcceptRequest $ShipmentDigest = $ShipmentConfirmResponse->ShipmentDigest; AcceptRequest($AccessLicenseNumber, $UserID, $Password, $CustomerContext, $ShipmentDigest, $ShipmentID, $connect); // After first successful request call a function which will send AcceptRequest } else { echo $ShipmentConfirmResponse->Response->Error->ErrorDescription; }
Итак, вот функция для отправки второго запроса «AcceptRequest». Это тот же код, но с другой структурой XML.
3. ShipmentAcceptRequest
function AcceptRequest ($AccessLicenseNumber, $UserID, $Password, $CustomerContext, $ShipmentDigest) { $RequestAction = "ShipAccept"; $domtree = new DOMDocument('1.0'); // <AccessRequest> $AccessRequest = $domtree->createElement("AccessRequest"); $domtree->appendChild($AccessRequest); // <AccessLicenseNumber> $AccessRequest->appendChild($domtree->createElement('AccessLicenseNumber', $AccessLicenseNumber)); // <UserId> $AccessRequest->appendChild($domtree->createElement('UserId', $UserID)); // <Password> $AccessRequest->appendChild($domtree->createElement('Password', $Password)); // </AccessRequest> // <ShipmentAcceptRequest> $ShipmentAcceptRequest = $domtree->createElement("ShipmentAcceptRequest"); $domtree->appendChild($ShipmentAcceptRequest); // <Request> $Request = $domtree->createElement("Request"); $ShipmentAcceptRequest->appendChild($Request); // <TransactionReference> $TransactionReference = $domtree->createElement("TransactionReference"); $Request->appendChild($TransactionReference); // <CustomerContext> $TransactionReference->appendChild($domtree->createElement('CustomerContext', $CustomerContext)); // </TransactionReference> // <RequestAction> $Request->appendChild($domtree->createElement('RequestAction', $RequestAction)); // </Request> // <ShipmentDigest> $ShipmentAcceptRequest->appendChild($domtree->createElement('ShipmentDigest', $ShipmentDigest)); // </ShipmentAcceptRequest>
И снова вы отправляете его с помощью этого кода. Затем вам нужно заглянуть в переменную $ response, если она была успешной или нет, и вы можете сохранить идентификатор отслеживания и цену доставки и делать с ним все, что хотите.
ВАЖНО: вы также получите изображение с кодировкой Base64 с кодировкой
4. Отгрузка
$domtree->preserveWhiteSpace = true; $domtree->formatOutput = true; $xml_string = $domtree->saveXML(); $url = 'https://wwwcie.ups.com/ups.app/xml/ShipAccept'; // Again testing URL $stream_options = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => "$xml_string", ), ); $context = stream_context_create($stream_options); $response = file_get_contents($url, null, $context); $ShipmentAcceptResponse = new SimpleXMLElement($response); if ((string)$ShipmentAcceptResponse->Response->ResponseStatusCode == 1) { $Tracking_ID = $ShipmentAcceptResponse->ShipmentResults->PackageResults->TrackingNumber; $Price = $ShipmentAcceptResponse->ShipmentResults->ShipmentCharges->TransportationCharges->MonetaryValue; $ImageBase64 = $ShipmentAcceptResponse->ShipmentResults->PackageResults->LabelImage->GraphicImage; } else { echo $ShipmentAcceptResponse->Response->Error->ErrorDescription; } }