Uncaught SoapFault exception: [HTTP] Bad Request - php

i am trying to make a Soap request i says this error :
Uncaught SoapFault exception: [HTTP] Bad Request in C:\
<?php
$client = new SoapClient(null, array('location' => "http://webservices.micros.com/ows/5.1/Availability.wsdl#FetchCalendar",
'uri' => "http://###.###.###.##:8080/ows_ws_51/Availability.asmx?wsdl"));
//print_r($client);
$para = array('StayDateRange' => array('StartDate' => '2013-10-01','EndDate' => '2013-10-10'),'GuestCount'=>array('GuestCountageQualifyingCode'=>'ADULT','GuestCountageQualifyingCode'=>'CHILD'));
$ns = 'http://webservices.micros.com/og/4.3/Availability/'; //Namespace of the WS.
//Body of the Soap Header.
$headerbody = array('OriginentityID' => 'OWS',
'DestinationentityID' => 'WEST',
'systemType'=>'WEB',
'systemType'=>'ORS');
//Create Soap Header.
$header = new SOAPHeader($ns, 'OGHeader', $headerbody);
//set the Headers of Soap Client.
$client->__setSoapHeaders($header);
$client->__soapCall("FetchCalendar", $para);
?>
whats wrong in my code ?
after adding Exception handling :
http://postimg.org/image/6w9lmac1d/
EDIT: XML Added
<?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>
<OGHeader transactionID="005435" timeStamp="2008-12-09T13:26:56.4056250-05:00" xmlns="http://webservices.micros.com/og/4.3/Core/">
<Origin entityID="OWS" systemType="WEB" />
<Destination entityID="WEST" systemType="ORS" />
</OGHeader>
</soap:Header>
<soap:Body>
<FetchCalendarRequest xmlns:a="http://webservices.micros.com/og/4.3/Availability/" xmlns:hc="http://webservices.micros.com/og/4.3/HotelCommon/" xmlns="http://webservices.micros.com/ows/5.1/Availability.wsdl">
<HotelReference chainCode="AXA" hotelCode="AXAMUM" />
<StayDateRange>
<hc:StartDate>2013-10-01</hc:StartDate>
<hc:EndDate>2013-10-10</hc:EndDate>
</StayDateRange>
<GuestCount>
<hc:GuestCount ageQualifyingCode="ADULT" count="1" />
<hc:GuestCount ageQualifyingCode="CHILD" count="0" />
</GuestCount>
</FetchCalendarRequest>
</soap:Body>
</soap:Envelope>

I believe there are 2 layer of problem, first you didn't catch the exception when it is thrown. second you request might be incorrect/invalid that's why it returns 400 bad request
try{
$client->__soapCall("FetchCalendar", $para);
}catch (SoapFault $exception){
//or any other handling you like
var_dump(get_class($exception));
var_dump($exception);
}

Related

PHP SOAP Header for Authentication Issue

I have the below SOAP XML, Im trying to access the vehicle details with the soap header authentication. I have tried the below code. I think, Im missing something on this. Can u help
SOAPAction: "http://abcddetails.org/getVehicleDetails"
<?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>
<UserIdentifierSoapHeaderIn xmlns="http://abcddetails.org/">
<UserName>string</UserName>
<Password>string</Password>
</UserIdentifierSoapHeaderIn>
</soap:Header>
<soap:Body>
<getVehicleDetails xmlns="http://abcddetails.org/">
<request>
<SystemCode>int</SystemCode>
<UserID>string</UserID>
<PlateInfo>
<PlateNo>long</PlateNo>
<PlateOrgNo>long</PlateOrgNo>
<PlateColorCode>int</PlateColorCode>
</PlateInfo>
<ChassisNo>string</ChassisNo>
</request>
</getVehicleDetails>
</soap:Body>
PHP code along with the SOAP Header, I have created as the below.
<?php
$wsdl = "http://abcddetails.org/InspectionServices.asmx?WSDL";
$client = new SoapClient($wsdl, array('trace'=>1)); // The trace param will show you errors stack
$auth = array(
'Username'=>'XXXXX',
'Password'=>'XXXXX',
);
$header = new SOAPHeader($wsdl, 'UserIdentifierSoapHeaderIn', $auth);
$client->__setSoapHeaders($header);
// web service input params
$request_param = array(
"SystemCode" => 4,
"UserID" => "TEST",
"ChassisNo" => '1N4AL3A9XHC214925'
);
$responce_param = null;
try
{
$responce_param = $client->getVehicleDetails($request_param);
//$responce_param = $client->call("webservice_methode_name", $request_param); // Alternative way to call soap method
}
catch (Exception $e)
{
echo "<h2>Exception Error!</h2>";
echo $e->getMessage();
}
print_r($responce_param);
?>
Can u guide if anything I have written wrong here in this.
You can use the __soapCall method like this:
$result = $client->__soapCall('webserviceMethodeName', ['parameters' => $params]);
In your case a soap action would be invoked like this:
$responce_param = $client->__soapCall('getVehicleDetails', ['parameters' => $request_param]);
Read more

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.

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.

how to create soap xml request in php

<?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>
<SubmitRequest xmlns="http://tripauthority.com/hotel">
<siteID>string</siteID>
<username>string</username>
<password>string</password>
<xmlFormattedString>string</xmlFormattedString>
</SubmitRequest>
</soap:Body>
</soap:Envelope>
we have to call above soap xml. i have siteID, username, password.
where string is below
<ArnRequest><Availability DisplayCurrency="USD" SearchTimeout="15"><HotelAvailability InDate="2007-04-26" OutDate="2007-04-29" Rooms="1" Adults="2" Children="0"><Hotel HotelID="8800"/></HotelAvailability></Availability></ArnRequest>
i have no idea on soap request. Please help with this to get response on above soap xml in PHP. The above xml is of ARN(Alliance reservations)
thanks in advance.
Calling webservices is quite easy, if you just want to send a prepared raw xml request. You could for instance use CURL for this.
Here the code which uses the php soapclient. I get "invalid credentials", but this should be ok as you'd put your valid ones in there.
<?
$string ='<ArnRequest><Availability DisplayCurrency="USD" SearchTimeout="15"><HotelAvailability InDate="2007-04-26" OutDate="2007-04-29" Rooms="1" Adults="2" Children="0"><Hotel HotelID="8800"/></HotelAvailability></Availability></ArnRequest>';
$xmlrequest = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hot="http://tripauthority.com/hotel">
<soapenv:Header/>
<soapenv:Body>
<hot:SubmitRequestDoc>
<!--Optional:-->
<hot:siteID>string</hot:siteID>
<!--Optional:-->
<hot:aUserName>string</hot:aUserName>
<!--Optional:-->
<hot:aPassword>string</hot:aPassword>
<!--Optional:-->
<hot:aRequestDoc>
'.$string.'
</hot:aRequestDoc>
</hot:SubmitRequestDoc>
</soapenv:Body>
</soapenv:Envelope>';
//Change this variables.
$location_URL = 'http://tripauthority.com/hotel.asmx';
$action_URL = "http://tripauthority.com/hotel/SubmitRequestDoc";
$client = new SoapClient(null, array(
'location' => $location_URL,
'uri' => "http://tripauthority.com/hotel",
'trace' => 1,
));
$order_return = $client->__doRequest($xmlrequest,$location_URL,$action_URL,1);
//Get response from here
print_r($order_return);
?>
I have solve my question if any body in future have any problem they this code works for him, please check-
<?php
error_reporting(E_ALL);
define('API_SITEID', $your_siteid);
define('API_USERNAME', $your_uname);
define('API_PASSWORD', $your_pass);
define('API_WSDL', 'http://tripauthority.com/hotel.asmx?WSDL');
ini_set("soap.wsdl_cache_enabled", "0");
$xmlReq = '<ArnRequest>
<Availability DisplayCurrency="USD" SearchTimeout="15">
<HotelAvailability InDate="2014-09-26" OutDate="2014-09-27" Rooms="1" Adults="1" Children="0">
<Hotel HotelID="8800"/>
</HotelAvailability>
</Availability>
</ArnRequest>';
echo '<form action="" method="post">
<strong>XML Request:</strong>
<p>
<textarea style="width:100%;height:400px;" id="xmlReq" name="xmlReq">'.$xmlReq.'</textarea>
</p>
<input type="submit" name="submit" id="submit" value="Test Request">
<input type="hidden" name="avail" id="avail" value="y">
</form>';
if($_POST['avail'] == "y") {
$xmlRes = doSoapRequest((($_POST['xmlReq']) ? $_POST['xmlReq'] : $xmlReq));
echo '<strong>XML Response:</strong>
<p>
<textarea style="width:100%;height:400px;" id="xmlRes" name="xmlRes">'.$xmlRes.'</textarea>
</p>';
}
function doSoapRequest($xmlReq) {
try {
$client = new SoapClient(API_WSDL);
return $client->SubmitRequestRpc(API_SITEID, API_USERNAME, API_PASSWORD, $xmlReq);
} catch(SoapFault $exception) {
return "Fault Code: {$exception->getMessage()}";
}
}
?>
Thanks
php has a built in Soap Client as of 5: http://php.net/manual/en/class.soapclient.php
$wsdl = '
<?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>
<SubmitRequest xmlns="http://tripauthority.com/hotel">
<siteID>string</siteID>
<username>string</username>
<password>string</password>
<xmlFormattedString>string</xmlFormattedString>
</SubmitRequest>
</soap:Body>
</soap:Envelope>
';
try {
$client = #new SOAPClient($wsdl); // or preferably, use a url for $wsdl
// Be sure to replace soapMethodToUse with a mouthed for this specific web service.
$response = $client->soapMethodToUse(array('key' => 'val')); // Any params for this method
} catch (Exception $e) {
echo $e->getMessage();
}
die(var_dump($response));
You may try to modify request with help of __doRequest function.
<?php
require_once("MySoapClient.php");
$client = new MySoapClient($wsdUrl,array(
'location' => "http://tripauthority.com/hotel",
'trace' => 1,
'cache_wsdl' => WSDL_CACHE_NONE
)
);
$parameters=array('siteID'=>'string','username'=>'string','password'=>'string');
$err=0;
try{
$info = $client->__soapCall("SubmitRequest",array($parameters));
}
catch (SoapFault $e) {
echo "<pre>faultcode: '".$e->faultcode."'</pre>";
echo "<pre>faultstring: '".$e->getMessage()."'</pre>";
$err=1;
}
if($err==0)
print_r($info);
else
echo $client->__getLastRequest();
?>
MySoapClient.php
<?php
class MySoapClient extends SoapClient
{
function __doRequest($request, $location, $action, $version, $one_way = 0) {
$request=str_replace('</SubmitRequest>','<xmlFormattedString>string</xmlFormattedString></SubmitRequest>',$request);
return $request;
}
}
?>

PHP Soap Request: XML Body missing in the request

Request XML
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://schemas.navitaire.com/WebServices/ISessionManager/Logon</Action>
<h:ContractVersion xmlns:h="http://schemas.navitaire.com/WebServices">330</h:ContractVersion>
</s:Header>
<s:Body>
<LogonRequest xmlns="http://schemas.navitaire.com/WebServices/ServiceContracts/SessionService">
<logonRequestData xmlns:d4p1="http://schemas.navitaire.com/WebServices/DataContracts/Session" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<d4p1:DomainCode>WWW</d4p1:DomainCode>
<d4p1:AgentName>API****</d4p1:AgentName>
<d4p1:Password>********</d4p1:Password>
<d4p1:LocationCode i:nil="true" />
<d4p1:RoleCode>APIB</d4p1:RoleCode>
<d4p1:TerminalInfo i:nil="true" />
</logonRequestData>
</LogonRequest>
</s:Body>
</s:Envelope>
The WSDL contains http://pastie.org/9263788
PHP Code
$options = array("soap_version"=> SOAP_1_1,
"trace"=>1,
"exceptions"=>0
);
$client = new SoapClient('https://trtestr3xapi.navitaire.com/sessionmanager.svc?wsdl',$options);
$header[] = new SoapHeader('http://schemas.microsoft.com/ws/2005/05/addressing/none','Action','http://schemas.navitaire.com/WebServices/ISessionManager/Logon',1);
$header[] = new SoapHeader('http://schemas.navitaire.com/WebServices','ContractVersion','330', false);
$client->__setSoapHeaders($header);
$params = array("LogonRequestData" => array("AgentName" => "API*****",
"Password" => "Pass****",
"RoleCode" => "APIB",
"DomainCode" => "WWW"));
try{
$h= $client->Logon($params);
print nl2br(print_r($h, true));
echo 'Request : <br/><xmp>',
$client->__getLastRequest();
echo '</xmp>';
}
catch(SoapFault $fault){
echo 'Request : <br/><xmp>',
$client->__getLastRequest(),
'</xmp><br/><br/> Error Message : <br/>',
$fault->getMessage();
}
XML Request Generated from __getLastRequest()
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://schemas.navitaire.com/WebServices/ServiceContracts/SessionService" xmlns:ns2="http://schemas.microsoft.com/ws/2005/05/addressing/none" xmlns:ns3="http://schemas.navitaire.com/WebServices">
<SOAP-ENV:Header>
<ns2:Action SOAP-ENV:mustUnderstand="1">http://schemas.navitaire.com/WebServices/ISessionManager/Logon</ns2:Action>
<ns3:ContractVersion>330</ns3:ContractVersion>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:LogonRequest/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
As you can see the XML Body is empty. How do I flood the body with the parameters?
Full output can bee seen here which is caught in the try{ } block http://pastie.org/9295467
In the output you can also see
//If i use soap version 1.1
[faultstring] => Bad Request
[faultcode] => HTTP
//If i use soap version 1.2
[faultstring] => Cannot process the message because the content type 'application/soap+xml; charset=utf-8; action="http://schemas.navitaire.com/WebServices/ISessionManager/Logon"' was not the expected type 'text/xml; charset=utf-8'.
[faultcode] => HTTP
Did you try switching to SOAP_1_2 because the error message looks like to be associated to this.
Otherwise, you could use a WSDL to php generator such as WsdlToPhp at wsdltophp.com
I had the same problem and manage to fix it using SOAP_1_1 without compression.
So just disable the compression.

Categories