Can enable I enable WS-RM (version 1.2) with NuSOAP client just like below? I tried this, but I cannot receive data from the API. Any ideas? Thanks.
$client = new nusoap_client($api_link, array('reliable' => 1.2 , 'useWSA' => TRUE) );
Full Code:
try {
include_once 'WebServiceSOAP/lib/nusoap.php';
$api_link = 'https://www.my-api.com/MYAPI.svc/SSL?wsdl';
$acode = '###';
$uname = '###';
$ttype = '###';
$ccode = '###';
$hpass = '###';
//setting xml request to api
$credentials = '<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/" xmlns:ezr="http://schemas.datacontract.org/2004/07/EzremitAPI.Entities">
<soapenv:Header/>
<soapenv:Body>
<tem:GetLocalRates>
<!--Optional:-->
<tem:credentials>
<!--Optional:-->
<ezr:AgentCode>'.$acode.'</ezr:AgentCode>
<!--Optional:-->
<ezr:HashedPassword>'.$hpass.'</ezr:HashedPassword>
<!--Optional:-->
<ezr:Username>'.$uname.'</ezr:Username>
</tem:credentials>
<!--Optional:-->
<tem:payincurcode>'.$ccode.'</tem:payincurcode>
<!--Optional:-->
<tem:transferType>'.$ttype.'</tem:transferType>
</tem:GetLocalRates>
</soapenv:Body>
</soapenv:Envelope>';
//creating soap object
$client = new nusoap_client($api_link, array('cache_wsdl' => WSDL_CACHE_NONE, 'soap_version' => SOAP_1_2) );
$client->soap_defencoding = 'UTF-8';
$soapaction = "http://tempuri.org/IRateAPI/GetLocalRates";
$xmlobjs = $client->send($credentials, $soapaction);
$err = $client->getError();
if ($err) {
echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
exit();
}
//print_r($client);
print_r($xmlobjs);
}
catch(Exception $e) {
echo $e->getMessage();
}
?>
I'm not very good at PHP and SOAP. Above code may have errors. Could you please check the code and give me your comments. I have made some amendments after searching Google.
Also, can I run this on PHP 5.4.42? When I run above code, I get below error now.
Constructor error
HTTP Error: Unsupported HTTP response status 415 Cannot process the message because the content type 'text/xml; charset=UTF-8' was not the expected type 'application/soap+xml; charset=utf-8'. (soapclient->response has contents of the response)
If anyone is looking for the answer to my above question, I found the solution with the help from #Marcel.
The answer to the question:
This code is wrong, don't use it.
$client = new nusoap_client($api_link, array('reliable' => 1.2 , 'useWSA' => TRUE) );
NuSoap client is outdated and not supporting to WS-RM. I had to use PHP inbuilt SOAP extension to get my project worked.
Full answer in here: SOAP Error in PHP: OperationFormatter encountered an invalid Message body
Thanks
Related
I've been failing to consume a soap webservice with soapClient /nusoap I believe the issue might be with my header but I'm not sure as the error I get back are pretty vague at times.
We've been able to get it to work in SoapUI, which adds some additional things to the header notably this:
<wsa:Action>http://www.property24.com/services/IP24ListingServiceWcf/FetchAgents</wsa:Action><wsa:To>https://www.exdev.property24.com/Services/P24ListingService46.svc</wsa:To>
Its being added in SoapUI automaticaly by checking: add default WSA:to under WS-A
inside the header it also has xmlns:wsa="http://www.w3.org/2005/08/addressing" I dont know how to correctly set this if possible.
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:ser="http://www.property24.com/services">
<soap:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
<ser:CredentialsHeader>
<!--Optional:-->
<ser:EMail>..</ser:EMail>
<!--Optional:-->
<ser:Password>..!</ser:Password>
<!--Optional:-->
<ser:UserGroupEmailId>1</ser:UserGroupEmailId>
</ser:CredentialsHeader>
<wsa:Action>http://www.property24.com/services/IP24ListingServiceWcf/FetchAgents</wsa:Action><wsa:To>https://www.exdev.property24.com/Services/P24ListingService46.svc</wsa:To></soap:Header>
<soap:Body>
<ser:FetchAgents>
<!--Optional:-->
<ser:agencyId>..</ser:agencyId>
</ser:FetchAgents>
</soap:Body>
</soap:Envelope>
I'm getting a bad request response which I believe could be due to the header
this is what I've tried:
$headerbody = array(
'EMail' => $username,
'Password' => $password,
'UserGroupEmailId'=> $groupId
);
$headers[] = new SoapHeader('https://www.exdev.property24.com/', 'wsa:Action', "http://www.property24.com/services/IP24ListingServiceWcf/$method");
$headers[] = new SoapHeader('https://www.exdev.property24.com/', 'wsa:To', "https://www.exdev.property24.com/Services/P24ListingService46.svc");
$headers[] = new SoapHeader('https://www.exdev.property24.com/', 'CredentialsHeader', $headerbody);
$client->__setSoapHeaders($headers);
and
$header_part = '<CredentialsHeader><EMail>....</EMail><Password>....</Password><UserGroupEmailId>1</UserGroupEmailId></CredentialsHeader>';
$header_part .= '<wsa:Action>http://www.property24.com/services/IP24ListingServiceWcf/$method</wsa:Action><wsa:To>https://www.exdev.property24.com/Services/P24ListingService46.svc</wsa:To>';
$soap_var_header = new SoapVar($header_part, XSD_ANYXML, null, null, null);
$soap_header = new SOAPHeader('http://www.w3.org/2005/08/addressing', 'wsa',
$soap_var_header);
$client->__setSoapHeaders($soap_header);
I've not had a lot of experience with Soap webservices and have been stuck on this for quite some time thanks guys
construct header object by concatenating with user credentials
$headerStr= '
<ser:CredentialsHeader>
<!--Optional:-->
<ser:EMail>'.$username.'</ser:EMail>
<!--Optional:-->
<ser:Password>'.$password.'</ser:Password>
<!--Optional:-->
<ser:UserGroupEmailId>'.$groupId.'</ser:UserGroupEmailId>
</ser:CredentialsHeader>
<wsa:Action>http://www.property24.com/services/IP24ListingServiceWcf/FetchAgents</wsa:Action><wsa:To>https://www.exdev.property24.com/Services/P24ListingService46.svc</wsa:To>
';
$header= new \SoapHeader('http://www.w3.org/2005/08/addressing',
'wsa',
new \SoapVar($headerStr, XSD_ANYXML),
true
);
then construct SoapClient and set headers and operation name with parameters
$soapclient = new SoapClient($serviceURL,$options);
$soapclient-> __setSoapHeaders($header);
parameters can be sent as dictionary but very important to chang "Request" to be identical to the same name in service contract
$param=array ("Request" => array('agencyId'=>'123'));
$response =$soapclient->FetchAgents( $param);
var_dump(json_encode($response));
echo '<br><br><br>';
$array = json_decode(json_encode($response), true);
print_r($array);
the complete code snippet will be
try {
$options = [
"soap_version" => SOAP_1_2, // SOAP_1_1
'trace' => 1,
'exception' => 1,
'keep_alive' => false,
'connection_timeout' => 500000
];
$headerStr = '
<ser:CredentialsHeader>
<!--Optional:-->
<ser:EMail>' . $username . '</ser:EMail>
<!--Optional:-->
<ser:Password>' . $password . '</ser:Password>
<!--Optional:-->
<ser:UserGroupEmailId>' . $groupId . '</ser:UserGroupEmailId>
</ser:CredentialsHeader>
<wsa:Action>http://www.property24.com/services/IP24ListingServiceWcf/FetchAgents</wsa:Action><wsa:To>https://www.exdev.property24.com/Services/P24ListingService46.svc</wsa:To></soap:Header>
';
$header = new \SoapHeader(
'http://www.w3.org/2005/08/addressing',
'ws',
new \SoapVar($headerStr, XSD_ANYXML),
true
);
$soapclient = new SoapClient($serviceURL, $options);
$soapclient->__setSoapHeaders($header);
$param = array('agencyId' => '123');
$response = $soapclient->FetchAgents($param);
// print the last XML request to compare with the working one in soapUI
echo "REQUEST:\n" . $soapclient->__getLastRequest() . "\n";
var_dump(json_encode($response));
echo '<br><br><br>';
$array = json_decode(json_encode($response), true);
print_r($array);
} catch (Exception $e) {
echo $e->getMessage();
}
I've been searching around for a solution to my problem, but to no avail. I'm trying to send a soap request through a Wordpress plugin using the following:
function soapRequest($soapUsername, $soapNonce, $soapDateTime, $soapPassword) {
$wsdl = 'http://www.beautyfort.com/api/wsdl/v2/wsdl.wsdl';
$trace = true;
$exceptions = false;
$client = new SoapClient($wsdl, array('trace' => $trace, 'exceptions' => $exceptions));
// Must be a stdClass (and not an array)
$auth = new stdClass();
$auth->Username = $soapUsername;
$auth->Nonce = $soapNonce;
$auth->Created = $soapDateTime;
$auth->Password = $soapPassword;
$header = new SoapHeader('http://www.beautyfort.com/api/', 'AuthHeader', $auth);
$client->__setSoapHeaders($header);
$xml_array['TestMode'] = 'true';
$xml_array['StockFileFormat'] = 'JSON';
$xml_array['SortBy'] = 'StockCode';
try {
$response = $client->GetStockFile($xml_array);
}
catch (Exception $e) {
log_me("Error!");
log_me($e -> getMessage());
log_me('Last response: '. $client->__getLastResponse());
}
log_me('Last request: '. $client->__getLastRequest());
log_me($response);
}
This produces the following request:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://www.beautyfort.com/api/">
<SOAP-ENV:Header>
<ns1:AuthHeader>
<ns1:Username>joetest</ns1:Username>
<ns1:Nonce>htflFfIKM4</ns1:Nonce>
<ns1:Created>2019-02-09T10:13:51.000Z</ns1:Created>
<ns1:Password>NGFjYTJiNzJmOWY2MzBmY2M2MjJkNjg1MDgyMWRjMzQxOGY1YTNjYQ==</ns1:Password>
</ns1:AuthHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:GetStockFileRequest>
<ns1:TestMode>true</ns1:TestMode>
<ns1:StockFileFormat>JSON</ns1:StockFileFormat>
<ns1:SortBy>StockCode</ns1:SortBy>
</ns1:GetStockFileRequest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
And I get an invalid credentials error. I've also been testing in SoupUI and the following request works:
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:api="http://www.beautyfort.com/api/">
<soapenv:Header>
<api:AuthHeader>
<api:Username>joetest</api:Username>
<api:Nonce>tJrsRlQt6i</api:Nonce>
<api:Created>2019-02-06T23:34:11.000Z</api:Created>
<api:Password>ZTBhMmE5OGY4YTNlZWIzZTE0ZTc2ZjZiZDBhM2RhMjJmNzAxNzYwZA==</api:Password>
</api:AuthHeader>
</soapenv:Header>
<soapenv:Body>
<api:GetStockFileRequest>
<api:TestMode>true</api:TestMode>
<api:StockFileFormat>JSON</api:StockFileFormat>
<!--Optional:-->
<api:FieldDelimiter>,</api:FieldDelimiter>
<!--Optional:-->
<api:StockFileFields>
<!--1 or more repetitions:-->
<api:StockFileField>StockCode</api:StockFileField>
<api:StockFileField>Category</api:StockFileField>
<api:StockFileField>Brand</api:StockFileField>
<api:StockFileField>Collection</api:StockFileField>
<api:StockFileField>Category</api:StockFileField>
</api:StockFileFields>
<api:SortBy>StockCode</api:SortBy>
</api:GetStockFileRequest>
</soapenv:Body>
</soapenv:Envelope>
Now the only differences I can see (apart from the optional fields) is the names of the namespace, and the use of the Xml tag at the top of the request. Both of these shouldn't matter right? I'd really appreciate your help on this as I've been scratching my head for ages.
Thank you in advance!
Your perfect just need to set UTC timezone and secret format like below:
base64 encoded(sha1(Nonce . Created . Secret))
Recently got access to a soap API for a hotel management service. They have provided documentation which shows a basic example of a request:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
<Auth xmlns="http://xxxx/xxxxAPI">
<FromSystemId ID="1">CompanyName</FromSystemId>
<UserName>username</UserName>
<Password>password</Password>
</Auth>
</soapenv:Header>
<soapenv:Body>
<GetRegions Timestamp="2016-04-11" Version="1.0" Lang="en"
xmlns="http://xxxx/xxxxAPI">
<Country Code="GB" />
</GetRegions>
</soapenv:Body>
</soapenv:Envelope>
They have also provided a list of functions in their documentation and the parameters required for each of the functions. But i am abit confused on how to perform a request as i have never used a soap API before. They also haven't provided a WSDL, does this matter?
Anyway, here is how i thought to try and perform a request
$soapURL = "http://xxxx/xxxxAPI" ;
$soapParameters = Array('login' => "username", 'password' => "password") ;
$soapFunction = "getRegions";
$soapFunctionParameters = Array('countrycode' => 'GB');
$soapClient = new SoapClient($soapURL, $soapParameters);
$soapResult = $soapClient->__soapCall($soapFunction,
$soapFunctionParameters) ;
if(is_array($soapResult) && isset($soapResult['someFunctionResult'])) {
// Process result.
} else {
// Unexpected result
if(function_exists("debug_message")) {
debug_message("Unexpected soapResult for {$soapFunction}: ".print_r($soapResult, TRUE)) ;
}
}
Am i going about this the right way? i am unable to test this right now as i haven't received my authentication but wanted to make a start on it now.
Any help would be great.
Here is a small example.
$opts = array(
'location' => 'http://xxxx/xxxxAPI',
'uri' => 'urn:http://test-uri/'
);
$client = new SOAPClient(null, $opts);
$headerData = array(
'FromSystemId' => 'CompanyName',
'UserName' => 'username',
'Password' => 'password',
);
// Create Soap Header.
$header = new SOAPHeader('http://xxxx/xxxxAPI', 'Auth', $headerData);
// Set the Headers of Soap Client.
$client->__setSoapHeaders($header);
$result = $client->__soapCall('getRegions', array('GB'));
// $return = $client->__soapCall('getRegions', array(new SoapParam(new SoapVar('GB', XSD_STRING), 'countryCode')));
var_dump($result);
They also haven't provided a WSDL, does this matter?
To be able to add the HEADER attributes they must be mentioned in WSDL. If they not exist in WSDL they WILL NOT appear as attributes but rather <item><key/><value/></item> elements.
Tipp: If you know how the request has to be and you have no WSDL, then try to generate the HTTP header and XML body manually and execute the request with CURL or Guzzle.
Example with Guzzle:
$soapContent = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
<Auth xmlns="http://xxxx/xxxxAPI">
<FromSystemId ID="1">CompanyName</FromSystemId>
<UserName>username</UserName>
<Password>password</Password>
</Auth>
</soapenv:Header>
<soapenv:Body>
<GetRegions Timestamp="2016-04-11" Version="1.0" Lang="en"
xmlns="http://xxxx/xxxxAPI">
<Country Code="GB" />
</GetRegions>
</soapenv:Body>
</soapenv:Envelope>';
$client = new GuzzleHttp\Client([
'headers' => [ 'SOAPAction' => '"urn:http://xxxx/xxxxAPI/#getRegions"' ]
]);
$response = $client->post('http://xxxx/xxxxAPI',
['body' => $soapContent]
);
echo $response;
I am trying to consume a PHP Soap service however I seem to have having trouble with a complex/abstract type.
This is the SOAP call generated using SOAP UI :-
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:lin="http://llu.webservices.opalonline.co.uk/LineCharacteristicsWS">
<soapenv:Header/>
<soapenv:Body>
<lin:GetLineCharacteristics>
<lin:request>
<!--Optional:-->
<lin:UserCredentials>
<!--Optional:-->
<!--Optional:-->
<lin:Username>testUser</lin:Username>
<lin:Password>testPass</lin:Password><lin:AgentID>1234</lin:AgentID>
</lin:UserCredentials>
<lin:RequestDetails xsi:type="lin:TelephoneNumberRequest" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<lin:TelephoneNumber>123456789</lin:TelephoneNumber>
</lin:RequestDetails>
<lin:UserConsent>Yes</lin:UserConsent>
<lin:ServiceType>MPF</lin:ServiceType>
</lin:request>
</lin:GetLineCharacteristics>
</soapenv:Body>
</soapenv:Envelope>
Here is my PHP code :-
$call = new StdClass();
$call->request = new StdClass();
$call->request->UserConsent = "Yes";
$call->request->ServiceType = "MPF";
$call->request->UserCredentials = new StdClass();
$call->request->UserCredentials->Username="testUser";
$call->request->UserCredentials->Password="testPass";
$call->request->UserCredentials->AgentID=1234;
$call->request->RequestDetails = new StdClass();
$call->request->RequestDetails->TelephoneNumber = "123456789";
$url = "https://llu.webservices.opalonline.co.uk/LineCharacteristicsWSV6/LineCharacteristicsWS.asmx?wsdl";
$client = new SoapClient($url, array('trace' => 1, exceptions=> 1,'soap_version' => SOAP_1_1));
$result = $client->GetLineCharacteristics($call);
echo $client->__getLastRequest();
echo $client->__getLastResponse();
When I run the code, the following error is generated :-
Fatal error: Uncaught SoapFault exception: [soap:Client] Server was unable to read request. ---> There is an error in XML document (2, 382). ---> The specified type is abstract: name='RequestType', namespace='http://llu.webservices.opalonline.co.uk/LineCharacteristicsWS', at http://llu.webservices.opalonline.co.uk/LineCharacteristicsWS'>. in /Users/jamesormerod/NetBeansProjects/fpdfDev/TestClass.php:23
Can anyone help?
To be able to send the request well formed with correct type and namespace, you must use both classes named as the required elements and a classmap that maps the elements to the classes.
The WsdlToPhp project can help you generate the classes and the classmap. You can use the project at wsdltophp.com.
Then if for example you generate the package with the name LineCharacteristics, you'll be able to send the request using this sample code:
$lineCharacteristicsServiceGet = new LineCharacteristicsServiceGet();
// sample call for LineCharacteristicsServiceGet::GetLineCharacteristics()
$details = new LineCharacteristicsStructTelephoneNumberRequest('+3363136363636');
$request = new LineCharacteristicsStructGetLineCharacteristicsRequest($details, LineCharacteristicsEnumUserConsentEnum::VALUE_YES, LineCharacteristicsEnumServiceTypeEnum::VALUE_MPF);
$userCredentials = new LineCharacteristicsStructCredentials(11111,'********','********');
$request->setUserCredentials($userCredentials);
$characteristics = new LineCharacteristicsStructGetLineCharacteristics($request);
$r = $lineCharacteristicsServiceGet->GetLineCharacteristics($characteristics);
echo implode("\r\n", array($lineCharacteristicsServiceGet->getLastRequestHeaders(),$lineCharacteristicsServiceGet->getLastRequest(),$lineCharacteristicsServiceGet->getLastResponseHeaders(),$lineCharacteristicsServiceGet->getLastResponse()));
if($r)
print_r($lineCharacteristicsServiceGet->getResult());
else
print_r($lineCharacteristicsServiceGet->getLastError());
I am new to SOAP and trying to figure out how to build a call to a SOAP server. Here is the definition of what I am trying to get:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:prov="http://bridgewatersystems.com/xpc/subscribermetering/service/provisioning/">
<soapenv:Header/>
<soapenv:Body>
<prov:GetMeteringStateRequest>
<subscriber subscriber-id="USERID" />
</prov:GetMeteringStateRequest>
</soapenv:Body>
</soapenv:Envelope>
Here is the PHP I am using to test (and not working of course):
$user_id = "REALIDHERE";
$parameters->subscriber_id = $user_id;
$parameters->MIN = "test";
$parameters->partition_key = "test";
try {
$client = new SoapClient("http://SOAPIP:32010/soap/services/SubscriberMeteringProvisionAPI.wsdl");
echo "trying...\n";
print( $client->GetMeteringState( new SoapParam("subscriber", $parameters ) ) );
} catch (SoapFault $e) {
//var_dump($e);
}
Any help on getting the call to GetMeteringState() to work would be great.
Thanks.
I'd recommend checking out the XML that is generated by your request. This is explained pretty well here, but here's an example:
// prepare SoapClient
$client = new SoapClient("http://.../SubscriberMeteringProvisionAPI.wsdl");
print( $client->GetMeteringState( new SoapParam("subscriber", $parameters ) ) );
// output the XML request
echo "<pre>".$client->__getLastRequest()."</pre>";