I need to post SOAP request to some server.
I know exactly that the right example of SOAP request as follows:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<CreateOrderBroker xmlns="http://tempuri.org/">
<shortApp xmlns:a="http://schemas.datacontract.org/2004/07/ScroogeCbformsService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:PIB>John Doe</a:PIB>
<a:agreeId>3155</a:agreeId>
<a:formId>55</a:formId>
<a:stateCode>1234567890</a:stateCode>
<a:telephone>1511528945</a:telephone>
</shortApp>
</CreateOrderBroker>
</s:Body>
</s:Envelope>
Also I have working C# example:
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
public EndpointAddress EndPointAddr {
get { return
new EndpointAddress("https://194.126.180.186:77/ScroogeCbForms.svc?wsdl");
}
}
private void btnSend_Click(object sender, EventArgs e)
{
ServicePointManager.ServerCertificateValidationCallback =
new RemoteCertificateValidationCallback(IgnoreCertificateErrorHandler);
ServicePointManager.Expect100Continue = false;
ServiceICreditTest.CreateOrderResponse response = new CreateOrderResponse();
ScroogeSiteGist client = new ScroogeSiteGist(Binding(), EndPointAddr);
shortApplicationBroker shortAp = new shortApplicationBroker()
{
agreeId = 3155,
PIB = "John Doe",
stateCode = "1234567890",
formId = 55,
telephone = "1511528945"
};
//response = client.CreateOrder("1012021013");
response = client.CreateOrderBroker(shortAp);
txtText.Text = string.Format("id = {0} ErrorId = {1}", response.OrderId, response.ReturnValue);
}
}
I'm trying to make same code in PHP 5.3:
<?php
$client = new SoapClient("https://194.126.180.186:77/ScroogeCbForms.svc?wsdl", array('soap_version' => SOAP_1_1, 'trace' => 1));
$params = array(
'agreeId' => 3155,
'PIB' => 'John Doe',
'stateCode' => '3289013768',
'formId' => 55,
'telephone' => '0661254877'
);
$client->CreateOrderBroker($params);
But request and callback from this code is next:
<?php
...
echo "REQUEST:<pre>".htmlspecialchars($client->__getLastRequest()) ."</pre>";
echo "CALLBACK:<pre>".htmlspecialchars($client->__getLastResponse())."</pre>";
REQUEST:
<?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:CreateOrderBroker/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
CALLBACK:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body><CreateOrderBrokerResponse xmlns="http://tempuri.org/"><CreateOrderBrokerResult xmlns:a="http://schemas.datacontract.org/2004/07/ScroogeCbformsService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:OrderId>0</a:OrderId>
<a:ReturnValue>Object reference not set to an instance of an object.</a:ReturnValue>
</CreateOrderBrokerResult>
</CreateOrderBrokerResponse>
</s:Body>
</s:Envelope>
It seems that body of request is empty.
What does it mean? If call made in wsdl-mode and request body is empty then wsdl-schema is broken, right?
If wsdl is broken what is the way to construct initial right SOAP request manually? Can anyone give an example?
Moreover, the data given in initial right SOAP request is enough to construct this request manually? Or I need some extra (namespaces, etc.)
Try the following code:
$client = new SoapClient("https://194.126.180.186:77/ScroogeCbForms.svc?wsdl", array('soap_version' => SOAP_1_1, 'trace' => 1));
class shortApp {
function __construct()
{
$this->agreeId = 3155;
$this->PIB = 'John Doe';
$this->stateCode = '3289013768';
$this->formId = 55;
$this->telephone = '0661254877';
}
}
$sa = new shortApp();
$shortApp = new SoapVar($sa, SOAP_ENC_OBJECT, 'shortApp', 'http://soapinterop.org/xsd');
$response = $client->CreateOrderBroker(new SoapParam($shortApp, 'shortApp'));
This code should give you 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://tempuri.org/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="http://soapinterop.org/xsd" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:CreateOrderBroker>
<shortApp xsi:type="ns2:shortApp">
<agreeId xsi:type="xsd:int">3155</agreeId>
<PIB xsi:type="xsd:string">John Doe</PIB>
<stateCode xsi:type="xsd:string">3289013768</stateCode>
<formId xsi:type="xsd:int">55</formId>
<telephone xsi:type="xsd:string">0661254877</telephone>
</shortApp>
</ns1:CreateOrderBroker>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Related
I'm trying connect Twilio autopilot to voice call with custom data. According to documentation there is Memory parameter where I can put custom data.
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Connect action="https://www.example.com/autopilot">
<Autopilot Memory={"CarModel":"Diablo","CarMake":"Lamborghini","CarYear":"2019"}>UAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</Autopilot>
</Connect>
</Response>
But when I try to put it via strong types I receive a message "Sorry an error occurred."
Here is the method:
public VoiceResponse ConnectAutopilot(Guid requestId)
{
var response = new VoiceResponse();
var autopilotUrl =
$"https://channels.autopilot.twilio.com/v1/{ AccountSid}/{AutopilotName }/twilio-voice";
var connect = new Connect
{
Action = new Uri(autopilotUrl)
};
var pilot = new Autopilot { Name = AutopilotName};
pilot.SetOption("Memory", new{requestId});
pilot.SetOption("TargetTask", "greeting");
connect.Append(pilot);
response.Append(connect);
response.Hangup();
return response;
}
I have an XML file (it's a SOAP request to an SAP ME Service).
The file looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:me="http://sap.com/xi/ME">
<soapenv:Header />
<soapenv:Body>
<me:ShopOrderByBasicDataQuery_sync>
<me:ShopOrderByBasicDataQuery>
<me:ShopOrder>BOXBUILD_LASER_TEST</me:ShopOrder>
<me:SiteRef>
<me:Site>TEST1</me:Site>
</me:SiteRef>
</me:ShopOrderByBasicDataQuery>
</me:ShopOrderByBasicDataQuery_sync>
</soapenv:Body>
</soapenv:Envelope>
Using Paste Special I used Paste XML as Classes and got a number of classes.
I the created a new Envelope object
Envelope shoporder = new Envelope();
EnvelopeBody shoporderBody = new EnvelopeBody();
ShopOrderByBasicDataQuery_sync shoporderSync = new ShopOrderByBasicDataQuery_sync();
ShopOrderByBasicDataQuery_syncShopOrderByBasicDataQuery shoporderDataQuery = new ShopOrderByBasicDataQuery_syncShopOrderByBasicDataQuery();
shoporderSync.ShopOrderByBasicDataQuery = shoporderDataQuery;
shoporderDataQuery.ShopOrder = "BOXBUILD_LASER_TEST";
shoporderDataQuery.SiteRef = new ShopOrderByBasicDataQuery_syncShopOrderByBasicDataQuerySiteRef();
shoporderDataQuery.SiteRef.Site = "TEST1";
shoporderSync.ShopOrderByBasicDataQuery = shoporderDataQuery;
shoporderBody.ShopOrderByBasicDataQuery_sync = shoporderSync;
shoporder.Body = shoporderBody;
string tmp = (string)SoapHelper.SerializeToXmlString(shoporder);
The string tmp contains this (after a bit of formatting)
<?xml version="1.0" encoding="utf-16"?>
<Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<Body>
<ShopOrderByBasicDataQuery_sync xmlns="http://sap.com/xi/ME">
<ShopOrderByBasicDataQuery>
<ShopOrder>BOXBUILD_LASER_TEST</ShopOrder>
<SiteRef>
<Site>TEST1</Site>
</SiteRef>
</ShopOrderByBasicDataQuery>
</ShopOrderByBasicDataQuery_sync>
</Body>
</Envelope>
Which is not a correct file! What have I done wrong?
I have been trying to add a header to SOAP request as follows
<soapenv:Header>
<UsernameToken xmlns="http://test.com/webservices">username</UsernameToken>
<PasswordText xmlns="http://test.com/webservices">password</PasswordText>
<SessionType xmlns="http://test.com/webservices">None</SessionType>
</soapenv:Header>
I have found suggestions to use SoapHeader to include header values, but introduces another level such as
<soapenv:Header>
<CustomHeader>
<UsernameToken xmlns="http://test.com/webservices">username</UsernameToken>
<PasswordText xmlns="http://test.com/webservices">password</PasswordText>
<SessionType xmlns="http://test.com/webservices">None</SessionType>
</CustomHeader>
</soapenv:Header>
Can anyone suggest how I can form a request without CustomHeader.
Try to use this one
private static void Main()
{
using (var client = new ServiceClient())
using (var scope = new OperationContextScope(client.InnerChannel))
{
MessageHeader usernameTokenHeader = MessageHeader.CreateHeader("UsernameToken",
"http://test.com/webservices", "username");
OperationContext.Current.OutgoingMessageHeaders.Add(usernameTokenHeader);
MessageHeader passwordTextHeader = MessageHeader.CreateHeader("PasswordText",
"http://test.com/webservices", "password");
OperationContext.Current.OutgoingMessageHeaders.Add(passwordTextHeader);
MessageHeader sessionTypeHeader = MessageHeader.CreateHeader("SessionType",
"http://test.com/webservices", "None");
OperationContext.Current.OutgoingMessageHeaders.Add(sessionTypeHeader);
string result = client.GetData(1);
Console.WriteLine(result);
}
Console.ReadKey();
}
The Service Trace viewer shows following
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<UsernameToken xmlns="http://test.com/webservices">username</UsernameToken>
<PasswordText xmlns="http://test.com/webservices">password</PasswordText>
<SessionType xmlns="http://test.com/webservices">None</SessionType>
<To s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://localhost:13332/Service1.svc</To>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IService/GetData</Action>
</s:Header>
</s:Envelope>
Take a look OperationContextScope for more info
i try to connect to web service. i success to get to the function 'AppendChunk', but with out headers, please Someone, tell me what i am doing worng?
the xml i have to send look like that:
<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>
<WSCredential xmlns="http://cellact.com/">
<Username>string</Username>
<Password>string</Password>
<Company>string</Company>
</WSCredential>
</soap:Header>
<soap:Body>
<AppendChunk xmlns="http://cellact.com/">
<FileName>string</FileName>
<buffer>base64Binary</buffer>
<Offset>long</Offset>
</AppendChunk>
</soap:Body>
</soap:Envelope>
this is the code:
require_once('/root/nusoap-0.9.5/lib/nusoap.php');
function GetFileContents( $filename ) {
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
return $contents;
}
$parameters = array("stream" => GetFileContents("bookSmall.csv"));
$client = new nusoap_client('http://cellactprolocal.net/mews/WSExt2.asmx?wsdl',true);
$params = array(
'FileName' => "book2.csv",
'buffer' => $parameters,
'Offset' => 0
);
$ns = "http://cellact.com";
$headers = "<WSCredential ><ns1:Username xmlns:ns1=\"$ns\">xxx</ns1:Username>
<ns2:Password xmlns:ns2=\"$ns\">xxx/</ns2:Password>
<ns3:Company xmlns:ns3=\"$ns\">xxx</ns3:Company>
</WSCredential >";
$client->setHeaders($headers);
$result = $client->call('AppendChunk', $params, $ns);
print_r($result);
You should encode your file before sending, first add function
function GetFileContents( $filename ) {
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
return $contents;
}
Then change in the code :
$decodeContent = base64_encode(GetFileContents("your file name"));
$params = array(
'FileName' => "test.csv",
'buffer' => $decodeContent,
'Offset' => 0
);
If i test the Service with SoapUi the operation scucceds, but if implement the Soap Client with PHP the parameters are allways emty and i get an NullReferenceException.
<?php
$options = array();
$options['classmap']['Abonnent'] = 'RequestType';
$client = new SoapClient('wsdllink',$options);
class RequestType
{
public $Email;
public $Nachname;
public $Passwort;
public $Vorname;
}
$abo = new RequestType;
$abo->Email = 'email';
$abo->Nachname = 'lastname';
$abo->Passwort = 'passwort';
$abo->Vorname = 'firstname';
try {
$result = $client->Abonnieren($abo);
} catch(SoapFault $e) {
echo "Request :\n". ($client->__getLastRequest()). "\n";
echo "Response :\n". ($client->__getLastResponseHeaders()). "\n";
echo "Response :\n". ($client->__getLastResponse()). "\n";
echo($e->getMessage());
} catch(Exception $e) {
echo $e->getMessage();
}
?>
var_dump($client->__getFunctions())
array(2) {
[0]=>
string(53) "AbonnierenResponse Abonnieren(Abonnieren $parameters)"
[1]=>
string(53) "RegisterIBResponse RegisterIB(RegisterIB $parameters)"
}
var_dump($client->__getTypes())
array(8) {
[0]=>
string(87) "struct Abonnent {
string Email;
string Nachname;
string Passwort;
string Vorname;
}"
[1]=>
string(41) "struct Abonnieren {
Abonnent abonnent;
}"
[2]=>
string(56) "struct AbonnierenResponse {
boolean AbonnierenResult;
}"
[3]=>
string(41) "struct RegisterIB {
Abonnent abonnent;
}"
[4]=>
string(56) "struct RegisterIBResponse {
boolean RegisterIBResult;
}"
[5]=>
string(8) "int char"
[6]=>
string(17) "duration duration"
[7]=>
string(11) "string guid"
}
XML from SoupUI
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:tem="http://tempuri.org/"
xmlns:prm="http://schemas.datacontract.org/2004/07/PRMarketService">
<soapenv:Header/>
<soapenv:Body>
<tem:Abonnieren>
<tem:abonnent>
<prm:Email>email</prm:Email>
<prm:Nachname>lastname</prm:Nachname>
<prm:Passwort>passwort</prm:Passwort>
<prm:Vorname>firstname</prm:Vorname>
</tem:abonnent>
</tem:Abonnieren>
</soapenv:Body>
</soapenv:Envelope>
XML from PHP Client
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://schemas.datacontract.org/2004/07/PRMarketService"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ns2="http://tempuri.org/">
<SOAP-ENV:Body>
<ns2:Abonnieren xsi:type="ns1:Abonnent">
<ns1:Email>email</ns1:Email>
<ns1:Nachname>lastname</ns1:Nachname>
<ns1:Passwort>passwort</ns1:Passwort>
<ns1:Vorname>firstname </ns1:Vorname>
</ns2:Abonnieren>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Compare messages sent by SoapUI and PHP's SoapClient, for example using Fiddler. If I recall correctly, you'll have to wrap the XML body PHP sends in a root element named after the appropriate SOAPAction.
Something like this:
class AbonnierenRequest
{
public $Abbonent;
}
class Abonnent
{
public $Email;
public $Nachname;
public $Passwort;
public $Vorname;
}
$request = new AbonnierenRequest();
$request->Abonnent = new Abonnent();
$request->Abonnent->Email = 'email';
$request->Abonnent->Nachname = 'lastname';
$request->Abonnent->Passwort = 'passwort';
$request->Abonnent->Vorname = 'firstname';
$result = $client->Abonnieren($request);