Last active
March 27, 2023 02:24
-
-
Save igorbenic/19684e12576b049f8443f16182bfe302 to your computer and use it in GitHub Desktop.
Working with PHP SoapClient | ibenic.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Trace On so we can easily debug XML we constructed as its returns it. | |
$client = new \SoapClient(SOAP_SERVICE_URL, array('trace' => 1) ); | |
$client->__setSoapHeaders( $header ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/"> | |
<SOAP-ENV:Header> | |
<ns1:AuthentificationHeader> | |
<ns1:UserName>{AuthUSERNAME}</ns1:UserName> | |
<ns1:Password>{AuthPASSWORD}</ns1:Password> | |
</ns1:AuthentificationHeader> | |
</SOAP-ENV:Header> | |
<SOAP-ENV:Body> | |
<ns1:CreateShipmentOrders> | |
<ns1:shipmentOrders> | |
<ns1:ShipmentOrder> | |
<ns1:SenderContact>{NAME}</ns1:SenderContact> | |
<ns1:ClientReferenceNumber>{OrderNumber}</ns1:ClientReferenceNumber> | |
<ns1:SenderPhone>{Phone}</ns1:SenderPhone> | |
<ns1:SenderCountry>{CountryCode}</ns1:SenderCountry> | |
<ns1:SenderCityName>{City}</ns1:SenderCityName> | |
<ns1:SenderCityPOCode>{PostalCode}</ns1:SenderCityPOCode> | |
<ns1:SenderStreet>{Street}</ns1:SenderStreet> | |
<ns1:RecipientContact>{RecipientName}</ns1:RecipientContact> | |
<ns1:RecipientPhone>{RecipientPhone}</ns1:RecipientPhone> | |
<ns1:RecipientEMail>{RecipientEmail}</ns1:RecipientEMail> | |
<ns1:RecipientCountry>{RecipientCountryCode}</ns1:RecipientCountry> | |
<ns1:RecipientCityName>{RecipientCity}</ns1:RecipientCityName> | |
<ns1:RecipientCityPOCode>{RecipientPostalCode}</ns1:RecipientCityPOCode> | |
<ns1:RecipientStreet>{RecipientAddress}</ns1:RecipientStreet> | |
<ns1:Value>{OrderValue}</ns1:Value> | |
<ns1:PackageList> | |
<ns1:Package> | |
<ns1:BarcodeClient>{BarcodeClient}</ns1:BarcodeClient> | |
<ns1:Barcode/> | |
<ns1:BarcodeType>{BarcodeType}</ns1:BarcodeType> | |
<ns1:Weight>{Weight}</ns1:Weight> | |
</ns1:Package> | |
</ns1:PackageList> | |
<ns1:AdditionalServiceList> | |
<ns1:AdditionalService> | |
<ns1:AdditionalServiceId>{ServiceID}</ns1:AdditionalServiceId> | |
</ns1:AdditionalService> | |
</ns1:AdditionalServiceList> | |
</ns1:ShipmentOrder> | |
</ns1:shipmentOrders> | |
<ns1:username>{username}</ns1:username> | |
<ns1:password>{password}</ns1:password> | |
</ns1:CreateShipmentOrders> | |
</SOAP-ENV:Body> | |
</SOAP-ENV:Envelope> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$headerBody = new \stdClass(); | |
$headerBody->UserName = '{AuthUSERNAME}'; | |
$headerBody->Password = '{AuthPASSWORD}'; | |
$header = new \SoapHeader( 'http://tempuri.org/', 'AuthentificationHeader', $headerBody ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// <username> && <password> inside of the Body. Adding such public attributes to objects constructs the nodes with same name. | |
// Later, we'll use this object to append more info. | |
$params = new \stdClass(); | |
$params->username = '{username}'; | |
$params->password = '{password}'; | |
// Code here that will add data to the object for SOAP | |
// Call SOAP: | |
// All $params data will be the data under <ns1:CreateShipmentOrders> in XML example | |
$result = $client->__soapCall('CreateShipmentOrders', [ 'CreateShipmentOrders' => $params ]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$orderInfo = [ | |
'SenderContact' => '{NAME}', | |
'ClientReferenceNumber' => '{OrderNumber}', | |
'SenderPhone' => '{Phone}', | |
'SenderCountry' => {CountryCode}, | |
'SenderCityName' => '{City}', | |
'SenderCityPOCode' => '{PostalCode}', | |
'SenderStreet' => '{Street}', | |
'RecipientContact' => '{RecipientName}', | |
'RecipientPhone'=> '{RecipientPhone}', | |
'RecipientEMail' => '{RecipientEmail}', | |
'RecipientCountry' => '{RecipientCountryCode}', | |
'RecipientCityName' => '{RecipientCity}', | |
'RecipientCityPOCode' => '{RecipientPostalCode}', | |
'RecipientStreet' => '{RecipientAddress}', | |
'Value' => '{OrderValue}', | |
]; | |
// This will hold data to put inside of <ns1:ShipmentOrder> | |
$shipmentOrder = new \stdClass(); | |
foreach ( $orderInfo as $nodeName => $nodeValue ) { | |
// Adding 'http://tempuri.org/' as namespace so we have the same n1 namespace on each node. | |
$shipmentOrder->{ $nodeName } = new \SoapVar( $nodeValue, XSD_STRING, null, null, null, 'http://tempuri.org/'); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Adding 'http://tempuri.org/' as namespace to all data so we have the same n1 namespace on each node. | |
// Object to hold data for <Package/> node. | |
$package = new \stdClass(); | |
// Adding <ns1:BarcodeClient> data. | |
$package->BarcodeClient = new \SoapVar( '{BarcodeClient}', XSD_STRING, null, null, null, 'http://tempuri.org/'); | |
// Adding <Barcode />. Since it's an empty node, we add it as SOAP_ENC_OBJECT instead of string. | |
$package->Barcode = new \SoapVar( new \stdClass(), SOAP_ENC_OBJECT, null, null, null, 'http://tempuri.org/'); | |
$package->BarcodeType = new \SoapVar( '{BarcodeType}', XSD_STRING, null, null, null, 'http://tempuri.org/'); | |
$package->Weight = new \SoapVar( '{Weight}', XSD_STRING, null, null, null, 'http://tempuri.org/'); | |
// Createing a packageList to hold the <Package/> node | |
$packageList = new \stdClass(); | |
// Adding attribute 'Package' so it is put in XML as the name of the node | |
$packageList->Package = new \SoapVar( $package, SOAP_ENC_OBJECT, null, null, null, 'http://tempuri.org/'); | |
// Adding attribute 'PackageList' so the data of packages is added to node <PackageList/> | |
$shipmentOrder->PackageList = new \SoapVar( $packageList, SOAP_ENC_OBJECT, null, null, null, 'http://tempuri.org/'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Adding 'http://tempuri.org/' as namespace to all data so we have the same n1 namespace on each node. | |
$additionalService = new \stdClass(); | |
// Adding <ns1:AdditionalServiceId> data. | |
$additionalService->AdditionalServiceId = new \SoapVar( '{ServiceID}', XSD_STRING, null, null, null, 'http://tempuri.org/'); | |
// Createing a AdditionalServiceList to hold the <AdditionalService/> node | |
$AdditionalServiceList = new \stdClass(); | |
$AdditionalServiceList->AdditionalService = new \SoapVar( $additionalService, SOAP_ENC_OBJECT, null, null, null, 'http://tempuri.org/'); | |
// Adding attribute 'AdditionalServiceList' so the data of packages is added to node <AdditionalServiceList/> | |
$shipmentOrder->AdditionalServiceList = new \SoapVar( $AdditionalServiceList, SOAP_ENC_OBJECT, null, null, null, 'http://tempuri.org/'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Adding 'http://tempuri.org/' as namespace to all data so we have the same n1 namespace on each node. | |
// Object to hold <ShipmentOrder/> node | |
$shipmentOrders = new \stdClass(); | |
$shipmentOrders->ShipmentOrder = new \SoapVar( $shipmentOrder, SOAP_ENC_OBJECT, null, null, null, 'http://tempuri.org/'); | |
// Adding the whole object that holds the data for ShipmentOrder to $params so it is added under node <shipmentOrders/> | |
$params->shipmentOrders = new \SoapVar( $shipmentOrders, SOAP_ENC_OBJECT, null, null, null, 'http://tempuri.org/'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class CreateShipmentOrdersResult { | |
protected $response = null; | |
public function __construct($object) { | |
$this->response = $object; | |
} | |
public function getResult() { | |
// SOAP Service returns the XML and has a CreateShipmentOrdersResult node. | |
return $this->response->CreateShipmentOrdersResult; | |
} | |
public function getResponse() { | |
// SOAP Service returns the XML and has a WSCreateShipmentOrdersResponse node | |
// inside of node CreateShipmentOrdersResult. | |
return $this->getResult()->WSCreateShipmentOrdersResponse; | |
} | |
public function __get( $key ) { | |
return $this->getResponse()->{ $key }; | |
} | |
public function isOK() { | |
return $this->ResponseStatus === 'OK'; | |
} | |
} | |
// $result is the object we received when doing the __soapCall. | |
$resultObject = new CreateShipmentOrdersResult( $result ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment