PHP API Soap Header Authentication - php

I have a small problem which I can't solve.
I'm trying to connect to a SOAP API (criteo) which works fine with SoapUI.
When I try to replicate the logic in e.g. php I get auth. errors.
I'm pretty sure that the header information are not passed correctly (I tried already several solutions which I found here without any result).
This is the request in SoapUI:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v20="https://advertising.criteo.com/API/v201010">
<soapenv:Header>
<v20:apiHeader>
<v20:authToken>12345</v20:authToken>
<v20:appToken>123456</v20:appToken>
</v20:apiHeader>
</soapenv:Header>
<soapenv:Body>
<v20:getReportDownloadUrl>
<v20:jobID>12345</v20:jobID>
</v20:getReportDownloadUrl>
</soapenv:Body>
</soapenv:Envelope>
In php I created the header like that:
/*
* wsdl: https://advertising.criteo.com/API/v201010/AdvertiserService.asmx?WSDL
*/
$authTokens = new stdClass();
$authTokens->authToken = 12345;
$authTokens->appToken = '123456';
$header = new SoapHeader('https://advertising.criteo.com/API/v201010/AdvertiserService.asmx', "apiHeader", $authTokens, true);
$client->__setSoapHeaders(array($header));
print_r($client->getAccount());
When I run the script I'll get an error:
Uncaught SoapFault exception: [soap:Receiver] Server was unable to process request. ---> Unable to cast object of type 'System.Web.Services.Protocols.SoapUnknownHeader' to type 'Criteo.WebService.DataModel.apiHeader'
Can somebody give me a hint ?
Thanks for the help.

Got the same problem. Change namespace value to https://advertising.criteo.com/API/v201010 from https://advertising.criteo.com/API/v201010/AdvertiserService.asmx

Related

SOAP request and repsonse with PHP (display response)

I need to make a SOAP request and get a response back, but I have no idea how SOAP works. I tried to search for it and everything is so confusing.
I need to make an authentication request here like this :
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:dir="http://stellatravelgateway.stellatravelservices.co.uk/DirectoryService">
<soapenv:Header/>
<soapenv:Body>
<dir:Authenticate>
<!-- Optional: -->
<dir:authenticateRequest BranchCode="abcde" UserName="user" Password="password" Application="application" Client="?">
<dir:BranchID>1</dir:BranchID>
</dir:authenticateRequest>
</dir:Authenticate>
</soapenv:Body>
</soapenv:Envelope>
And get an response after that, but have no idea how to do it. I searched and found some similar questions, but can not get any response.
What I am doing is this :
$send = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:dir="http://stellatravelgateway.stellatravelservices.co.uk/DirectoryService">
<soapenv:Header/>
<soapenv:Body>
<dir:Authenticate>
<!-- Optional: -->
<dir:authenticateRequest BranchCode="abcde" UserName="user" Password="password" Application="application" Client="?">
<dir:BranchID>1</dir:BranchID>
</dir:authenticateRequest>
</dir:Authenticate>
</soapenv:Body>
</soapenv:Envelope>';
$soapUrl ="http://devapi.stellatravelgateway.stellatravelservices.co.uk/DirectoryService.svc?singleWsdl";
$soapClientVar = new SoapClient("http://devapi.stellatravelgateway.stellatravelservices.co.uk/DirectoryService.svc?singleWsdl");
$resp = $soapClientVar->Authenticate($send);
var_dump($resp);
I know that 99% I totally wrong of what should I do. Can someone please help me understand what exactly should I do and make this SOAP work?
TIA
The WSDL sets up the SoapClient:
http://devapi.stellatravelgateway.stellatravelservices.co.uk/DirectoryService.svc?singleWsdl
and tells the client what to expect in terms of methods to call on the SOAP service. SoapClient takes care of creating what you see in $send.
Instead of sending raw SOAP (which SoapClient will do for you), you work at the method level. From the WSDL the Authenticate() method takes a parameter of type tns:AuthenticateRequest which contains BranchCode, UserName etc. and returns an object of type tns:AuthenticateResponse, containing tns:ResultBase which contains the actual result Success, Narrative etc.
this might get you towards your solution:
$soapClientVar = new SoapClient("http://devapi.stellatravelgateway.stellatravelservices.co.uk/DirectoryService.svc?singleWsdl");
$params = array(
"BranchCode" => $BranchCode,
"UserName" => $UserName,
"Password" => $Password,
"Application" => $Application,
"Client" => $Client
);
$response = $soapClientVar->__soapCall("Authenticate", array($params));

How to create soap wsdl request template using php

this my xml request which i want to create for service.
<soapenv:Header>
<aut:AuthenticationHeader>
<aut:LoginName>John</aut:LoginName>
<aut:Password>Johnpass</aut:Password>
<aut:Culture>en_US</aut:Culture>
<aut:Version>8</aut:Version>
</aut:AuthenticationHeader>
</soapenv:Header>
<soapenv:Body>
<hot:GetCancellationPolicies>
<hot:hotelId>19752</hot:hotelId>
<hot:hotelRoomTypeId>20955</hot:hotelRoomTypeId>
<hot:dtCheckIn>2017-06-25</hot:dtCheckIn>
<hot:dtCheckOut>2017-06-28</hot:dtCheckOut>
</hot:GetCancellationPolicies>
and that is my php code
$p = new stdClass;
$p->request->HotelId = '19752';
$p->request->HotelRoomTypeId = '20955';
$p->request->dtCheckIn = '2017-06-25';
$p->request->dtCheckOut = '2017-06-28';
$quote = $client->GetCancellationPolicies($p);
actually i am getting error.which is mentioned below.
Error Details:
SOAP-ERROR: Encoding: object has no 'nResId' property
i'm new in web services it would be great if someone could help me out of it.
from the wsdl, you should generate the corresponding PHP sdk using a WSDL to php generator such as the PackageGenerator project as you won't wonder how to construct the request, in addition, it'll ease you handling the response as everything is OOP

Generating schema with soap PHP client

How can I generate this schema using php soap client cant seem to get a way
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:loc="http://www.csapi.org/schema/parlayx/subscribe/manage/v1_0/local">
<soapenv:Header>
<tns:RequestSOAPHeader xmlns:tns="http://www.namesapace.com/common/v2_1">
<tns:one>***</tns:one>
<tns:two>***</tns:three>
<oauth_token>***</oauth_token>
</tns:RequestSOAPHeader>
</soapenv:Header>
<soapenv:Body>
<loc:ProductRequest>
<subInfo>
<productID>***</productID>
<isAutoExtend>0</isAutoExtend>
</subInfo>
</loc:ProductRequest>
</soapenv:Body>
</soapenv:Envelope>
On top of using the client, sometimes it may not bind to the namespace especially if using different soap versions or other strange reasons. Solved it by overriding the soap class and replacing the ns1 and ns2 values as described here http://php.net/manual/en/soapclient.dorequest.php#74123
IF no success with php soapclient, just post the request with curl.
Example in this answer - SOAP request in PHP with CURL

Fetch PHP SOAP request data

I have a page which will receive data directly from a SOAP request. The problem is "How will I receive that data?". If data is coming directly in $_GET parameter, I can fetch via $_GET['PARAM'], but how will I fetch data here as it is coming directly in SOAP?
I have a WSDL file, NotificationToCP.wsdl as well.
Following is the SOAP request.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sub="http://example.com">
<soapenv:Header/>
<soapenv:Body>
<sub:handleNewSubscription>
<custAttributesDTO>
<cpId>{$cpId}</cpId>
<cpPwd>{$cpPwd}</cpPwd>
<productId>{$productId}</productId>
<channelName>{$channelName}</channelName>
<contentProvider>{$contentProvider}</contentProvider>
<cpName>{$cpName}</cpName>
<firstConfirmationDTTM>{$firstConfirmationDTTM}</firstConfirmationDTTM>
<secondConfirmationDTTM>{$secondConfirmationDTTM}</secondConfirmationDTTM>
</custAttributesDTO>
</sub:handleNewSubscription>
</soapenv:Body>
</soapenv:Envelope>
I am trying to fetch this SOAP request data with following code.
$client = new SoapClient($_SERVER['DOCUMENT_ROOT'] . "/test/NotificationToCP.wsdl", array('trace' => true));
var_dump($client->__getLastResponse());
I can't figure out where I am going wrong.
What you are returning from the SOAP call is an Object and you cannot print an Object you need to pull the value of the property of that object that stores your return value that you can then print. So in the SOAP XML where your response tag is, use the value of that tag and then:
print_r($result->name_Of_Tag_In_XML);

returning soap xml response for a soap server php

I am trying to create a soap service in php using native php soap server. I have already prepared the wsdl file.
There are basically four methods that can be called with the soap service. The input soap request for one of the request ShowRemittanceDetail is shown below.
<soap-env:body>
<ns1:showremittancedetailrequest>
<username>admin</username>
<password>pass</password>
<refno>USA1956127848</refno>
</ns1:showremittancedetailrequest>
</soap-env:body>
Anyway the soap request does not have a header and I have just shown the body here. I have no problem parsing the soap request. The response should look like this
<?xml version="1.0" encoding="ISO-8859-1"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns2:responseType xmlns:ns2="http://tempuri.org/response">
<code>00</code>
<message>Transaction does not exist or is not available</message>
<responseBody>
<responseStr>4</responseStr>
</responseBody>
</ns2:responseType>
</soapenv:Body>
</soapenv:Envelope>
This is a particular response which is generated when the transaction with the reference number is not available in the server.
I have received the soap request and evaluated it. However I have a problem with the return type of the soap response. I cannot generate a valid response. What type of response should a soap server return?
Things I have tried:
I have tried returning an xml string. But the soap client request throws an exception with the following message.
looks like we got no xml document.
I have also tried returing a native php SoapVar() with the same result.
I have tried returning an object response that is specified in the classmap fo the soap server.
e.g. for the example above, I have tried returning a ShowRemittanceDetailResponse object with the same result. (looks like we got no xml document).
I have tried returning a DomDocument Object . The exception thrown in this case is
the encoded object does not have a responseStr property.
I have tried returning an stdClass object with the same fields as the response expects with similar result.
Please help me.
Thanks in advance.
The solution I found was returning a SoapVar object. I didnt have soapui and the client I wrote in php was incorrect. Hence, I had problem verifying the returned xml as the php soap client was throwing an exception. The correct way for me was to return a SoapVar.

Categories