This is my SOAP request:
<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:impl="http://impl.user">
<soapenv:Header/>
<soapenv:Body>
<impl:UserSessionDetails soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<in0 xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">requstXml</in0>
<in1 xsi:type="impl:UserInfo">
<password xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">pwd</password>
<userName xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">pwd</userName>
</in1>
</impl:UserSessionDetails>
</soapenv:Body>
</soapenv:Envelope>
I've tried the following PHP code:
$soapClient = new SoapClient("http://example.com/services/?WSDL");
$sh_param = array(
'userName' => 'user',
'Password' => 'pwd'
);
$headers = new SoapHeader('http://tempuri.org/', false);
$soapClient->__setSoapHeaders(array($headers));
$requestXML = '<request name= "UserSessionDetails"><UserDetails><user_name>username</user_name><from_date>fromdate</from_date><to_date>to_date</to_date><group></group></UserDetails></request>';
$result = $soapClient->UserSessionDetails(array('in0'=> $requestXML, 'in1'=> $sh_param));
$simple = $result->$simple = $result->UserSessionDetailsResponse;
$p = xml_parser_create();
xml_parse_into_struct($p, $simple, $vals, $index);
xml_parser_free($p);
echo "<pre>";
print_r($vals);
echo "</pre>"
But it returns nothing. Can anybody please tell me what the problem is?
Do not use raw xml in SoapClient requests. Use objects/arrays or SoapVar instead.
Basic example:
<?php
$client = new \SoapClient('http://example.com/services/?WSDL');
// Optionally your service endpoint
$client->__setLocation('http://example.com/services');
$userDetails = new stdClass();
$userDetails->user_name = 'username';
$userDetails->from_date = 'fromdate';
$userDetails->to_date = 'to_date';
$in0 = new stdClass();
$in0->UserDetails = $userDetails;
$in1 = new stdClass();
$in1->userName = 'pwd';
$in1->Password = 'pwd';
$request = new stdClass();
$request->in0 = $in0;
$request->in1 = $in1;
$result = $client->UserSessionDetails($request);
You can debug you SOAP services with any SOAP client like SOAPUI
P.S.
Have a look at Zend_Soap_Client and SoapVar examples
Related
I have XML document:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<saml2p:ArtifactResolve xmlns:saml2p="urn:oasis:names:tc:SAML:2.0:protocol" Version="2.0"
IssueInstant="2020-06-01T10:25:15+02:00">
<saml2:Issuer xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion">TEST</saml2:Issuer>
<saml2p:Artifact>AAQAAKFbFR94fxqmioAqjJUwfHTFVHTDBTVHdBwwTW+ehcM19zsk=</saml2p:Artifact>
</saml2p:ArtifactResolve>
</soapenv:Body>
</soapenv:Envelope>
I try to do this in this way:
$results = array();
$filename = 'cert.p12';
$password = 'certpass';
$priv_key = openssl_pkcs12_read(file_get_contents($filename), $results, $password);
$doc = new DOMDocument();
$doc->loadXML($xml);
$xp = new DOMXPath($doc);
$xp->registerNamespace('soapenv', 'http://schemas.xmlsoap.org/soap/envelope/');
$xp->registerNamespace('saml2p','urn:oasis:names:tc:SAML:2.0:protocol');
$xp->registerNamespace('saml2','urn:oasis:names:tc:SAML:2.0:assertion');
$xp->registerNamespace('ds',XMLSecurityDSig::XMLDSIGNS);
$artifactResolveNode = $xp->query('/*[local-name()=\'Envelope\']/*[local-name()=\'Body\']/*[local-name()=\'ArtifactResolve\']')->item(0);
if($artifactResolveNode){
$objDSig = new XMLSecurityDSig();
$objDSig->setCanonicalMethod(XMLSecurityDSig::EXC_C14N);
$objDSig->addReference(
$artifactResolveNode,
XMLSecurityDSig::SHA256,
array('http://www.w3.org/2000/09/xmldsig#enveloped-signature', 'http://www.w3.org/2001/10/xml-exc-c14n#'),
array('force_uri' => true)
);
$objKey = new XMLSecurityKey(XMLSecurityKey::RSA_SHA256, array('type' => 'private'));
$objKey->loadKey($results['pkey'], FALSE);
$objDSig->sign($objKey);
$objDSig->add509Cert($results['cert']);
$objDSig->appendSignature($doc->documentElement());
echo $doc->saveXML();
}
Output with Singnature in wrong location is:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<saml2p:ArtifactResolve xmlns:saml2p="urn:oasis:names:tc:SAML:2.0:protocol" Version="2.0" IssueInstant="2020-06-01T10:25:15+02:00" Id="pfx97783ab1-0339-0f2e-b759-9e9c07e347b0">
<saml2:Issuer xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion">TEST</saml2:Issuer><saml2p:Artifact>AAQAAKFbFR94fxqmioAqjJUwfyUtjJbv0uEPB7ooopodBwwTW+ehcM19zsk=</saml2p:Artifact></saml2p:ArtifactResolve>
</soapenv:Body>
<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<ds:SignedInfo>
(...)
</soapenv:Envelope>
I want to put Signature node between ISSUER and ARTIFACT. This location is very important to correct send SOAP Envelope.
It is possible?
I found!
I need add code below:
$artifactNode = $xp->query('/*[local-name()=\'Envelope\']/*[local-name()=\'Body\']/*[local-name()=\'ArtifactResolve\']/*[local-name()=\'Artifact\']')->item(0);
and append Signature like this:
$objDSig->insertSignature($artifactResolveNode,$artifactNode);
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))
I am trying to create this :
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<AccessKey xmlns="http://eatright/membership" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Value>67a4ef47-9ddf-471f-b6d0-0000c28d57d1</Value>
</AccessKey>
</s:Header>
<s:Body>
<WebUserLogin xmlns="http://eatright/membership">
<loginOrEmail>1083790</loginOrEmail>
<password>thomas</password>
</WebUserLogin>
</s:Body>
</s:Envelope>
I created this PHP code
class ChannelAdvisorAuth
{
public $AccessKey ;
public function __construct($key)
{
$this->AccessKey = $key;
}
}
$AccessKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$url = "http://ws.eatright.org/service/service.svc?wsdl";
$client = new SoapClient($url, array("trace" => 1, "exception" => 0));
$auth = new ChannelAdvisorAuth($AccessKey);
$header = new SoapHeader("AccessKey", "Value", $AccessKey, false);
$client->__setSoapHeaders($header);
$result = $client->ValidateAccessKey();
echo "REQUEST:\n" . $client->__getLastRequest() . "\n";
The output of the above PHP code is :
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://eatright/membership" xmlns:ns2="AccessKey">
<SOAP-ENV:Header>
<ns2:Value>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</ns2:Value>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:ValidateAccessKey/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
How to change the PHP code to output the XML as requested by web service provider?
How to replace the "SOAP-ENV" to "S"?
Is there is a way to remove the NS1 and NS2? and also adjust the whole format of the XML to meet the requirements? thanks
You don't need to worry about SOAP-ENV, ns1 or ns2 - they are just prefixes referring to the namespaces. As long as the full namespaces are correct, it's going to be alright.
I think the SOAP header should be made like this:
$access_key = new stdClass();
$access_key->Value = 'XXX';
$hdr = new SoapHeader('http://eatright/membership', 'AccessKey', $access_key);
$client->__setSoapHeaders($hdr);
I don't see a purpose of xmlns:i in the first example - there are no elements having XSI attributes.
I'm not sure what to do with the body. In your first example, there is a call to the WebUserLogin operation, while in your PHP code you are trying to call ValidateAccessKey.
Have you tried reading the WSDL file which is pointed by $url
Ok I found the problem and I will add it here in case someone looking for same issue.
$access_key = new stdClass();
$access_key->Value = 'xxxxxxxxxxxxxxxxxxx';
// Create the SoapClient instance
$url = "http://ws.eatright.org/service/service.svc?wsdl";
$client = new SoapClient($url, array("trace" => 1, "exception" => 0));
$hdr = new SoapHeader('http://eatright/membership', 'AccessKey', $access_key);
$client->__setSoapHeaders($hdr);
$soapParameters = array('loginOrEmail ' => $username, 'password' => $password);
$login = new stdClass();
$login->loginOrEmail='LoginID';
$login->password='Password';
$result = $client->WebUserLogin($login);
The problem is that I cannot succeed to send an array of class through SOAP to a specific web service that I cannot change his structure.
Here below you'll see the schema of the Soap function:
http://www.elyotech.com/share/yoni/forum/developpeznet-soap.jpg
Here you'll find the code I'm corresponding to it:
$s = new soapclient($URL_WEBSERVICE,array('wsdl'=>true,"trace"=>true));
$params = new stdClass();
$params->loginInfo = new stdClass();
$params->loginInfo->UserName = 'username';
$params->loginInfo->Password = 'password';
$params->loginInfo->LanguageCode = 'en';
$params->reservation = new stdClass();
$params->reservation->EmailRecipient2 = 'yonia#yopmail.com';
$params->reservation->EmailRecipient3 = 'yonia2#yopmail.com';
$params->reservation->FirstName = 'Yoni'
foreach($extras as $id=>$extra)
{
$params->Extras[$id]= new stdClass();
$params->Extras[$id]->SelectedExtra = new stdClass();
$params->Extras[$id]->ExtensionData = new stdClass();
$params->Extras[$id]->SelectedExtra->Amount = 1;
$params->Extras[$id]->SelectedExtra->ID = $extra;
}
Here is the SOAP request that is sent:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/">
<SOAP-ENV:Body>
<ns1:CreateReservation>
<ns1:loginInfo>
<ns1:UserName>monusername</ns1:UserName>
<ns1:Password>monpass</ns1:Password>
<ns1:LanguageCode>en</ns1:LanguageCode>
</ns1:loginInfo>
<ns1:reservation>
<ns1:EmailRecipient2>yonia#yopmail.com</ns1:EmailRecipient2>
<ns1:EmailRecipient3>yonia2#yopmail.com</ns1:EmailRecipient3>
<ns1:FirstName>Yoni</ns1:FirstName>
</ns1:reservation>
</ns1:CreateReservation>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
In this Soap request, I cannot find the Extras array of objects.
Do you have any idea of how I should create my object in order to send it correctly?
I've spend hours with SOAPClient and still get no result, hope someone can help me. I'm trying to create and send to web-service following code:
POST /PortalServices/PortalServices.asmx HTTP/1.1
Host:***.***.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://***.com/CreateUser"
<?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>
<UserCredentials xmlns="http://***.com/">
<UserName>string</UserName>
<PassWord>string</PassWord>
<ServiceName>string</ServiceName>
</UserCredentials>
</soap:Header>
<soap:Body>
<CreateUser xmlns="http://***.com/">
<user>
<Name>string</Name>
<Email>string</Email>
<PassWord>string</PassWord>
<WsServices>
<WsService>
<Id>int</Id>
</WsService>
</WsServices>
</user>
</CreateUser>
</soap:Body>
</soap:Envelope>
To get this xml I use the following code:
<?php
class WsService {
public $Id = "****";
}
class WsServices {
public $WsService;
public function __construct() {
$this->WsService = new WsService();
}
}
class WsUser {
public $Name = 'Sasha Lavasov';
public $Email = 'imsashko#gmail.com';
public $PassWord = 'test';
public $WsServices;
public function __construct() {
$this->WsServices = new WsService();
}
}
$ini = ini_set("soap.wsdl_cache_enabled","0");
$soap_url = 'http://***.***.com/PortalServices/PortalServices.asmx?wsdl';
$soapClient = new \SoapClient($soap_url, array(
'soap_version'=>SOAP_1_2,
'trace' => 1,
'exceptions' => true,
'cache_wsdl' => WSDL_CACHE_NONE
));
$hdd = new stdClass();
$hdd->UserName = '***';
$hdd->PassWord = '***';
$hdd->ServiceName = '***';
$hd = new SoapVar($hdd, SOAP_ENC_OBJECT);
$headers = new \SoapHeader('http://***.***.com/PortalServices', 'UserCredentials', $hd);
// Prepare Soap Client
$soapClient->__setSoapHeaders(array($headers));
$user = new WsUser();
// Setup the CreateUser parameters
$ap_param = array(
'Name' => 'Sasha Lavasov',
'WsUser' => $user
);
$param = new \SoapParam($user, 'WsUser');
// Call CreateUser ()
$error = 0;
try {
//Build user entity
$usr = new WsUser();
$wsServices = new StdClass();
$id = new StdClass();
$id->Id = '***';
$wsServices->WsService = $id;
$usr->WsServices = $wsServices;
$createUser = new stdClass();
$createUser->user = $usr;
$createUser = new SoapVar($createUser, SOAP_ENC_OBJECT, "WsUser", "http://***.***.com/PortalServices/PortalServices.asmx?op=CreateUser");
$pm = new SoapParam($createUser, "user");
$soapClient->CreateUser($pm);
//Here I get xml
var_dump( $soapClient->__getLastRequest()); die();
} catch (\SoapFault $fault) {
$error = 1;
print($fault->faultcode." - ".$fault->faultstring); die();
}
if ($error == 0) {
// DO SOMETHING
} else {
}
$res = $soapClient->__getLastResponse();
print_r($res); die();
But instead I get something like this:
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://***.***.com/PortalServices/PortalServices.asmx?op=CreateUser" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://***.com/" xmlns:ns3="http://***.***.com/PortalServices">
<env:Header>
<ns3:UserCredentials>
<UserName>***</UserName>
<PassWord>***</PassWord>
<ServiceName>***</ServiceName>
</ns3:UserCredentials></env:Header>
<env:Body>
<ns2:CreateUser xsi:type="ns1:WsUser">
<user>
<Name>Sasha Lavasov</Name>
<Email>imsashko#gmail.com</Email>
<PassWord>test</PassWord>
<WsServices><WsService><Id>***</Id></WsService></WsServices>
</user>
</ns2:CreateUser>
</env:Body>
</env:Envelope>
And get FALSE as result.
In the list of server's functions (using __getFunctions()) I've got following:
CreateUserResponse CreateUser(CreateUser $parameters)
Can anyone help me or just tell where I'm wrong?
Just a suggestion.
In your good xml you've:
<UserCredentials xmlns="http://***.com/">
but in your PHP you set:
$headers = new SoapHeader('http://***.***.com/PortalServices', 'UserCredentials', $hd);
Then you've 2 different namespaces: http://***.***.com/PortalServices instead of http://***.com/