Connect to sample SOAP 1.1 with PHP - php

Hello i need to connect to SOAP web services with password, username and SourceId in php.
the SOAP request is:
POST /webservices/AgentOnlineReservation.asmx HTTP/1.1
Host: 54.228.189.53
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/GetHotelsData"
<?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>
<GetHotelsData xmlns="http://tempuri.org/">
<SourceId>string</SourceId>
<UserName>string</UserName>
<Password>string</Password>
</GetHotelsData>
</soap:Body>
</soap:Envelope>
Please help me.

Use the native SoapClient library:
$client = new SoapClient('linkhere.com/AgentOnlineReservation.asmx?wsdl');
$response = $client->GetHotelsData(array(
'SourceId' => '...',
'UserName' => '...',
'Password' => '...'
));
print_r($response);
The link passed in the SoapClient call is the WSDL (Web Service Description Language) file.

Related

How to authenticate a SOAP service using PHP

I tried to authenticate the following request using PHP but its not working
Here is my following SOAP request
POST /BCWebservice/BCWebService.asmx HTTP/1.1
Host: service.bcstaging.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.bcstaging.com/bc/Authenticate"
<?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>
<Authenticate xmlns="http://www.bcstaging.com/bc">
<username>string</username>
<password>string</password>
<version>string</version>
<eaCode>int</eaCode>
</Authenticate>
</soap:Body>
</soap:Envelope>
Here is my PHP Code
$soapURL = "http://service.bcstaging.com/BCWebService.asmx?wsdl";
$soapParameters = array(
'Username' => $username,
'Password' => $password,
'ProductVersion' => '2.9.0.0',
'EACode' => $eaCode
);
$soapClient = new SoapClient($soapURL);
$objResponse = $soapClient->__soapCall('Authenticate', $soapParameters);
And I got the following error
Fatal error: Uncaught SoapFault exception: [soap:Server] Server was unable to process request. ---> Object reference not set to an instance of an object.
Any help would be appreciated. Thanks in advance.

How to create SOAP 1.2 request

I need help with creating SOAP 1.2 request. All i have is this:
SAMPLE REQUEST:
POST /WS/PriceList.asmx HTTP/1.1
Host: gateway.systemb2b.com
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<GetProducts xmlns="http://gateway.systemb2b.com/schemas/Product" />
</soap12:Body>
</soap12:Envelope>
SAMPLE RESPONSE:
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<GetProductsResponse xmlns="http://gateway.systemb2b.com/schemas/Product">
<GetProductsResult>xml</GetProductsResult>
</GetProductsResponse>
</soap12:Body>
</soap12:Envelope>
Thanks a lot.
<?php
$client = new SoapClient("URL/OF/YOUR/WSDL", array('soap_version' => SOAP_1_2));
$result = $client('GetProducts');
?>
I hope this example would help you to get your issue solved. All the best!
// SOAP 1.2 client
$options = array('soap_version'=>SOAP_1_2, 'exceptions'=>true, 'trace'=>1);
$client = new SoapClient('https://www.example.com/example.svc/SSL?wsdl', $options);
$actionHeader = new SoapHeader('http://www.w3.org/2005/08/addressing','Action','http://tempuri.org/xxxx/GetResult');
$toActionHeader = new SoapHeader('http://www.w3.org/2005/08/addressing','To','https://www.example.com/example.svc/SSL?wsdl');
$headerbody = array('Action' => $actionHeader,'To' => $toActionHeader);
$client->__setSoapHeaders($headerbody);
$client->__setLocation('https://www.example.com/example.svc/SSL');
$params = array( "param1"=>"abc", "param2"=>"ab123",c"param3"=>"ab1111" );
$client->__soapCall("GetResult", array($params));

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 send SOAP request in curl

I am a Beginner of soap, How do i send soap request? i searched in google and tried different methods but sadly it didn't worked for me.
i really appreciated your help.
here's the sample request that i should send:
POST /Universal/WebService.asmx HTTP/1.1
Host: www.sample.com
Content-Type: text/xml;charset="utf-8"
Content-Length: length
SOAPAction: https://www.sample.com/Universal/WebService.asmx/Request
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<soap:Envelope xmlns:xsi="http://wwww.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Request xmlns="https://www.sample.com/Universal/WebService.asmx">
<ID>CPHK01</ID>
<UID>TEST</UID>
<PWD>TEST</PWD>
<target_mpn>09183530925</target_mpn>
<amount>115</amount>
<ref_no>20060830143030112</ref_no>
<source_mpn>67720014</source_mpn>
</Request>
</soap:Body>
</soap:Envelope>
here's the 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://wwww.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<RequestResponse xmlns="https://www.sample.com/Universal/WebService.asmx">
<RequestResult>
<Ref_no>20060830143030112</Ref_no>
<StatusCode>101</StatusCode>
</RequestResult>
</RequestResponse>
</soap:Body>
</soap:Envelope>
PHP has a native SoapClient class. The constructor takes the WSDL as an argument. This is preferred to cURL because the SoapClient handles all of the SOAP intricacies and it allows you to deal with native objects and arrays and removes the need to manually build the SOAP envelope and XML.
try {
$client = new SoapClient('https://www.sample.com/Universal/WebService.asmx?wsdl');
$response = $client->Request(array(
'ID' => 'xxxx',
'UID' => 'xxxx',
'PWD' => 'xxxx',
'target_mpn' => 'xxxx',
'amount' => 'xxxx',
'ref_no' => 'xxxx',
'source_mpn' => 'xxxx'
));
print_r($response); // view the full response to see what is returned
// or get the response properties:
echo $response->RequestResult->Ref_no;
echo $response->RequestResult->StatusCode;
} catch (Exception $e) {
echo $e->getMessage();
}

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