PHP sending SOAP request with SOAP client - php

It's my first time with SOAP, I already read all manuals and still stacked with the following example:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Header>
<AuthenticationInfo xmlns="https://blabla.com/ws/Quoting">
<strUserName>[username]</strUserName>
<strPassword>[password]</strPassword>
</AuthenticationInfo>
</soap:Header>
<soap:Body>
<GetQuotes xmlns="https://blabla.com/ws/Quoting">
<CallerClientID>0</CallerClientID>
<quoteRequest>
<CapacityCode>2</CapacityCode>
<BabiesCount>0</BabiesCount>
<QuotingOptions>0</QuotingOptions>
</quoteRequest>
<clientIDs>
<int>20295</int>
</clientIDs>
<withUrlParam>false</withUrlParam>
</GetQuotes>
</soap:Body>
</soap:Envelope>
I do not understand how to start the soapclient with authorization, please help.

You need to use SoapHeader. Below is the example.
$ns = 'https://blabla.com/ws/Quoting'; //Namespace of the WS.
//Body of the Soap Header.
$headerbody = array('strUserName' => $someUser,
'strPassword' => $somePass);
//Create Soap Header.
$header = new SOAPHeader($ns, 'AuthenticationInfo', $headerbody);
//set the Headers of Soap Client.
$soap_client->__setSoapHeaders($header);

Related

Difference between two soap requests

My SOAP Request
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://ws.dgpys.deloitte.com" xmlns:ns2="ws.apache.org/namespaces/axis2">
<env:Header>
<ns2:ServiceGroupId>
<BOGUS>urn:uuid:7C2F61BDE7CB9D9C6D1424938568724</BOGUS>
</ns2:ServiceGroupId>
</env:Header>
<env:Body>
<ns1:getGunlukParametreRapor>
<date>2015-02-22T00:00Z</date>
</ns1:getGunlukParametreRapor>
</env:Body>
</env:Envelope>
Expected SOAP Request
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:ws="http://ws.dgpys.deloitte.com">
<soap:Header>
<axis2:ServiceGroupId xmlns:axis2="http://ws.apache.org/namespaces/axis2">urn:uuid:479731898147E116AD1424691518968</axis2:ServiceGroupId>
</soap:Header>
<soap:Body>
<ws:getGunlukParametreRapor>
<date>2015-02-22T00:00Z</date>
</ws:getGunlukParametreRapor>
</soap:Body>
</soap:Envelope>
Tried with following codes:
$options = array(
'trace' => 1,
'exceptions' => 1,
'soap_version' => SOAP_1_2
);
$client = new SoapClient("http://dgpysws.pmum.gov.tr/dgpys/services/EVDServis.wsdl", $options);
$p1 = new stdCLass();
$p1->loginMessage = new stdCLass();
$p1->loginMessage->UserName = new stdCLass();
$p1->loginMessage->UserName->v = "Username";
$p1->loginMessage->Password = new stdCLass();
$p1->loginMessage->Password->v = "Passwor";
$client->login($p1);
$headers[] = new SoapHeader('http//ws.apache.org/namespaces/axis2', 'ServiceGroupId', "UNIQUEID", false);
$client->__setSoapHeaders($headers);
$result = $client->getGunlukParametreRapor(array('date' => '2015-02-22T00:00Z'));
Question is:
These SOAP requests are same?
I'm using SOAP_1_2 and it should be like Expected SOAP Request but my request doesnt looks like to expected format. Missing where?
How can i get the output like as expected?
Note: dgpysws.pmum.gov.tr wsdl address is private area.
They are not the same. To get rid of the BOGUS node you need to use this:
$strHeaderComponent_Session = "<SessionHeader><ServiceGroupId>$theVarWithTheIDGoesHere</ServiceGroupId></SessionHeader>";
$objVar_Session_Inside = new SoapVar($strHeaderComponent_Session, XSD_ANYXML,
null, null, null);
$objHeader_Session_Outside = new SoapHeader('http//ws.apache.org/namespaces/axis2',
'SessionHeader', $objVar_Session_Inside);
// More than one header can be provided in this array.
$client->__setSoapHeaders(array($objHeader_Session_Outside));
try the following
$ns = 'http//ws.apache.org/namespaces/axis2'; //Namespace of the WS.
//Body of the Soap Header.
$headerbody = array('ServiceGroupId' => $UNIQUEID_Token);
//Create Soap Header.
$header = new SOAPHeader($ns, 'axis2', $headerbody);
//set the Headers of Soap Client.
$soap_client->__setSoapHeaders($header);
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://ws.dgpys.deloitte.com" xmlns:ns2="ws.apache.org/namespaces/axis2">
<env:Header>
<ns2:ServiceGroupId>
urn:uuid:7C2F61BDE7CB9D9C6D1424938568724
</ns2:ServiceGroupId>
</env:Header>
<env:Body>
<ns1:getGunlukParametreRapor>
<date>2015-02-22T00:00Z</date>
</ns1:getGunlukParametreRapor>
</env:Body>
</env:Envelope>
And
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:ws="http://ws.dgpys.deloitte.com">
<soap:Header>
<axis2:ServiceGroupId xmlns:axis2="http://ws.apache.org/namespaces/axis2">urn:uuid:479731898147E116AD1424691518968</axis2:ServiceGroupId>
</soap:Header>
<soap:Body>
<ws:getGunlukParametreRapor>
<date>2015-02-22T00:00Z</date>
</ws:getGunlukParametreRapor>
</soap:Body>
</soap:Envelope>
Are the same.
env=soap, ns2=ws and ns2=axis2. You can have any prefix to refer to these namespaces as you like. Once you assign the prefix you just refer to it using that in the other places. Only diff was the bogus tag tin first request. Just remove that.

Formatting namespace in soap header using PHP

I need to construct an XML SOAP request header using PHP where the output looks like this:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Header>
<AuthTokenHeader xmlns="SPP.net/ContractData">
<Token>B1912A8BA7AB6E56D688244D7B</Token>
</AuthTokenHeader>
</soap:Header>
....but my PHP is producing this instead:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="SPP.net/ContractData">
<SOAP-ENV:Header>
<ns1:AuthTokenHeader>
<Token>373161D1C28</Token>
</ns1:AuthTokenHeader>
</SOAP-ENV:Header>
Here is my PHP code:
// define namespace
$NameSpace = 'SPP.net/ContractData';
// build AuthTokenHeader vars
$authTokenVar[] = new SoapVar($TOKEN, XSD_STRING, null, null, 'Token');
// wrap in AuthTokenHeader tags
$header = new SoapVar($authTokenVar, SOAP_ENC_OBJECT, null, null, 'AuthTokenHeader');
// build soap header
$soapHeader = new SoapHeader($NameSpace, 'AuthTokenHeader', $header , false);
// instanciate soap client
$wsdlUrl= "https://gmwstest.sppinc.net/VPP/ContractExchange.asmx?wsdl";
$client = new SoapClient($wsdlUrl,
array(
"trace" => 1,
"exceptions" => 0,
"cache_wsdl" => 0));
// set soap headers
$client->__setSoapHeaders($soapHeader);
Here is the entire XML soap request I need to build:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Header>
<AuthTokenHeader xmlns="SPP.net/ContractData">
<Token>BEB40F7FD43168017ACF3772A91F2B7C2B37A722F6421598D0B61CCD3DBF8928CA18B25970AB6D43FD31A761F15D84A52C9FC315BE9F708F30EE4F08DAB4C74F94894A7D3242102831FB8303684F7FE492A2249CB35FB50030C905DD91F771103C5A1D486CCEA9AAB0D42AC37252B2E350B4E3F24CEE8D0B8CFCE04F1DD7D1B4D7C2A4F1912A8BA7AB6E56D688244D7B</Token> <!--GET TOKEN FROM LOGONRESULT SESSION VARIABLE-->
</AuthTokenHeader>
</soap:Header>
<soap:Body>
<SendContractData xmlns="SPP.net/ContractData">
<contract>
<ContractID>VPP00253221</ContractID> <!--GET ContractID FROM CONTRACT RESPONSE SESSION VARIABLE-->
<AccountNumber/>
<Status>Pending</Status>
<Company>VPP</Company>
<ContractPrefix/>
<DealerCode>10456</DealerCode>
<FirstName>FirstName</FirstName>
<Mi/>
<LastName>Lastname</LastName>
<PhoneNumber>(312)980-6000</PhoneNumber>
<Address1>303 Anytown Lane</Address1>
<Address2>Unit A</Address2>
<City>Yourtown</City>
<StateProvinceAbbr xsi:type="StateEnum">IL</StateProvinceAbbr>
<ZipCode>60601</ZipCode>
<Country>USA</Country>
<OriginatingDepartment>Other</OriginatingDepartment>
<Language>English</Language>
<VIN>1HGCM56705AJMD227</VIN>
<PurchasePrice>2000.00</PurchasePrice>
<SalesTax>0.00</SalesTax>
<DownPayment>200.00</DownPayment>
<AmountFinanced>1800.00</AmountFinanced>
<PolicyType>ServiceContract</PolicyType>
<InServiceDate>2010-03-12T00:00:00</InServiceDate>
<FirstPaymentDate xsi:nil="true"/>
<DealerCost>500</DealerCost>
<ContractHoldersInitialsCoordinates xsi:nil="true"/>
<ContractHoldersInitialsDateCoordinates xsi:nil="true"/>
<ContractHoldersSignatureCoordinates xsi:nil="true"/>
<SignatureDateCoordinates xsi:nil="true"/>
<SellersSignatureCoordinates xsi:nil="true"/>
<PurchaseDate>2014-06-23T14:24:46</PurchaseDate>
<BeginMileage>43650</BeginMileage>
<TermMonths>84</TermMonths>
<TermMileage>100000</TermMileage>
<NumberOfPayments>12</NumberOfPayments>
<CallBackURL>http://NewWarrantyVehicle_Example/Success.php</CallBackURL>
</contract>
</SendContractData>
</soap:Body>
</soap:Envelope>
The two XML snippets you presented are logically the same. Both reference the XML namespace for the AuthTokenHeader element differently, but in terms of a correctly working XML parser they express the same.
You cannot change the way PHP creates the XML of a Soap request. If the created request does not work with the server you have to use, please be more specific what problems you encounter.

how to call web services using soap in php

The following is a sample SOAP 1.1 request and response.:
POST /atservices/1.5/atws.asmx HTTP/1.1
Host: webservices2.autotask.net
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://autotask.net/ATWS/v1_5/getZoneInfo"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<getZoneInfo xmlns="http://autotask.net/ATWS/v1_5/">
<UserName>string</UserName>
</getZoneInfo>
</soap:Body>
</soap:Envelope>
we want to call web services of autotask using soap in php.can we get example for it
how we should call soap client.
Its output should be like this :
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<getZoneInfoResponse xmlns="http://autotask.net/ATWS/v1_5/">
<getZoneInfoResult>
<URL>string</URL>
<ErrorCode>int</ErrorCode>
<DataBaseType>string</DataBaseType>
<CI>int</CI>
</getZoneInfoResult>
</getZoneInfoResponse>
</soap:Body>
</soap:Envelope>
Use the PHP native SoapClient along with the service WSDL, like so:
$atservices_wsdl = "https://www.autotask.net/atservices/1.5/atws.wsdl";
$atservices_client = new SoapClient($atservices_wsdl);
$zone_info = $atservices_client->getZoneInfo("SomeUserName");
print_r($zone_info); // review the returned object converted from SOAP response.
echo $zone_info->DataBaseType; // this might work if it's not behind a Response object.
At the very least, you should be aiming for something like this. More can be found here.
$soap = new SoapClient('link/to/.wsdl');
$result = $soap->__soapCall('getZoneInfo', array('UserName' => $username));
var_dump($result);

how to call soap server using nusoap

i need to call a soap server, they provide request format like this (please see this page https://book.mylimobiz.com/api/ApiService.asmx?op=Test)
POST /api/ApiService.asmx HTTP/1.1
Host: book.mylimobiz.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "https://book.mylimobiz.com/api/Test"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Test xmlns="https://book.mylimobiz.com/api">
<apiId>string</apiId>
<apiKey>string</apiKey>
</Test>
</soap:Body>
</soap:Envelope>
I am using php nusoap to send request, and here is ho i am trying
require_once('lib/nusoap.php');
$serverPath = "https://book.mylimobiz.com/api/ApiService.asmx";
$param = array("apiId"=>"someapi","apiKey"=>"somekYE");
$client = new SoapClient($serverPath);
$tt = $client->call("Test",$param,"https://book.mylimobiz.com/api","https://book.mylimobiz.com/api/Test");
which is not working
can some one guide me how to request using nusoap or some thing else.
Thanks
You can do it like this:
$client = new SoapClient('https://book.mylimobiz.com/api/ApiService.asmx?WSDL');
$params = array();
$params["apiId"] = apiId;
$params["apiKey"] = apiKey;
$result = $client->Test($params);

Consume a .Net web service using PHP

This is my first time with web services/SOAP...i have been trying to consume .Net web services using PHP but to no avail. I have searched and read all pages that google throws up for anything related to this but i am still lost.
The thing is the SOAP service i am trying to call has an authorization header and i can't figure out a way to authenticate my request.
I have tried the php-soapclient and NuSoap both but there is no sample code available that would help. So any help would be great.
The following is a sample SOAP 1.1 request and response.
POST /OxiWalletService/Service.asmx HTTP/1.1
Host: 172.160.0.49
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/WS_GetData"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<AuthHeader xmlns="http://tempuri.org/">
<UserName>string</UserName>
<Password>string</Password>
</AuthHeader>
</soap:Header>
<soap:Body>
<WS_GetData xmlns="http://tempuri.org/">
<xmlString>string</xmlString>
</WS_GetData>
</soap:Body>
</soap:Envelope>
Response
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<WS_GetDataResponse xmlns="http://tempuri.org/">
<WS_GetDataResult>string</WS_GetDataResult>
</WS_GetDataResponse>
</soap:Body>
</soap:Envelope>
Can anybody please gimme a sample code on how to consume such a service.
Many thanks in advance!
This is the code that i have used to call the web service
<?php
$soap_client = new SoapClient("http://172.160.0.49/OxiWalletService/Service.asmx?WSDL");
$Uid='oxigen';
$Pwd='oxigen';
$ns = "http://tempuri.org/";
//Body of the Soap Header.
$headerbody = array('UserName' => $Uid,
'Password' => $Pwd
);
//Create Soap Header.
$header = new SOAPHeader($ns, 'AuthHeader', $headerbody);
//set the Headers of Soap Client.
$soap_client->__setSoapHeaders($header);
$par="<Wallet><SPName>AuthenticateMerchantWebVending</SPName><Parameters><Parameter><Name>#Account</Name><Size>50</Size><Value>1135600016</Value><Type>varchar</Type></Parameter><Parameter><Name>#Password</Name><Size>20</Size><Value>0OgknrdonyM=</Value><Type>varchar</Type></Parameter></Parameters><ParameterCount>2</ParameterCount><DataBase>1</DataBase></Wallet>";
$param=array('xmlString'=>$par);
$result=$soap_client->__SoapCall('WS_GetData',$param);
print_r ($result);
?>
and i am getting the following as output:
stdClass Object ( [WS_GetDataResult] => 2Unknown Error )
Ideas??
So it turns out you've to pass the second argument with parameters as the key of the array
meaning this
$result=$soap_client->__SoapCall('WS_GetData',$param);
should be
$result=$soap_client->__SoapCall('WS_GetData',array('parameters'=>$param));
This works now.
I think this should do the trick:
www.php.net/manual/en/soapclient.setsoapheaders.php
$ns = "http://tempuri.org/"
//Body of the Soap Header.
$headerbody = array('UserName' => $yourUsername,
'Password' => $yourPassword,
);
//Create Soap Header.
$header = new SOAPHeader($ns, 'AuthHeader', $headerbody);
//set the Headers of Soap Client.
$soap_client->__setSoapHeaders($header);

Categories