Create SOAP Request and Display Response - php

How do you send SOAP data and then display the response you get via PHP? I do have the WSDL file. I just want to learn how to send and show the response.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ser="http://service.application.example.com/">
<soapenv:Header/>
<soapenv:Body>
<ser:addPlace>
<arg0>
<addDate>2015-02-04</addDate>
<ClientID>98745</ClientID>
</arg0>
</ser:addPlace>
</soapenv:Body>
</soapenv:Envelope>

You can use SoapClient in php (http://php.net/manual/en/soapclient.soapclient.php)
<?php
$client = new \SoapClient("http://urlToWsdl/soap.wsdl", array("trace" => 1, "exception" => 0));
$params = array('arg0' => array('addDate' => '2015-02-04', 'ClientID' => 98745));
$result = $client->__soapCall('addPlace', $params);
var_dump($result);

Related

Testing SOAP web service FROM SOAP UI and PHP

I have been testing a web service with SOAP UI.
This what I put in SOAP UI:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:b2b="http://www.dominio.com.emision.request">
<soapenv:Header/>
<soapenv:Body>
<b2b:EmitirDocumentoRequest>
<documento>Q0FCfDkwMTE4NzYxNXw5MDA0MDkyMjR8Rn</documento>
<tipoDocumento>F</tipoDocumento>
<empresa>FAC</empresa>
<cuenta>FAC00111</cuenta>
<usuario>USR_WS_FAC00111</usuario>
<!--Optional:-->
<serie>SETT</serie>
<folio>5223</folio>
<monto>1078232.00</monto>
<fecha>2019-11-04</fecha>
<hora>07:40:08</hora>
</b2b:EmitirDocumentoRequest>
</soapenv:Body>
</soapenv:Envelope>
From SOAP UI works and the data is received correctly in those web services.
Now when I write this to PHP:
<?php
$factura = base64_encode(file_get_contents('FA002.txt'));
$client = new SoapClient("http://xxx.xxx.xxx.xxx:xxx/wsdl?configName=EmitirDocumento");
$datos = array(
'documento' => $factura,
'tipoDocumento' => 'F',
'empresa' => 'FAC',
'cuenta' => 'FAC00111',
'usuario' => 'USR_WS_FAC00111',
'serie' => 'SETT',
'folio' => '5224',
'monto' => 1078232.00,
'fecha' => '2019-11-04',
'hora' => '07:40:08'
);
$rs = $client->__soapCall('execute_EmitirDocumento', array($datos));
var_dump($rs);
Shows that is succesful, but the document is not sent to the web service, is there any mistake in my code?

Create an php array for sent to SOAP service

I have hml which works fine in SOAPUI:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/"
xmlns:wssc="http://schemas.datacontract.org/2004/07/WSSC.V4.DMS.EKV.WssDocsService">
<soapenv:Header/>
<soapenv:Body>
<tem:CreateDocument>
<!--Optional:-->
<tem:parameters>
<!--Optional:-->
<!--Optional:-->
<wssc:DocType>01</wssc:DocType>
<!--Optional:-->
<!--Optional:-->
<!--Optional:-->
<wssc:FieldValues>
<wssc:BaseDocumentField xsi:type="wssc:DocumentField"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!--Optional:-->
<wssc:Name>value1</wssc:Name>
<!--Optional:-->
<wssc:Value>email#email.email</wssc:Value>
</wssc:BaseDocumentField>
<wssc:BaseDocumentField xsi:type="wssc:DocumentField"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!--Optional:-->
<wssc:Name>content</wssc:Name>
<!--Optional:-->
<wssc:Value>just text</wssc:Value>
</wssc:BaseDocumentField>
<wssc:BaseDocumentField xsi:type="wssc:DocumentField"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!--Optional:-->
<wssc:Name>name</wssc:Name>
<!--Optional:-->
<wssc:Value>number</wssc:Value>
</wssc:BaseDocumentField>
</wssc:FieldValues>
<wssc:UserMail>email#email.email</wssc:UserMail>
</tem:parameters>
</tem:CreateDocument>
</soapenv:Body>
</soapenv:Envelope>
I need to create an array to repeat its structure.
Now I have such an array, but it does not work to the full:
<?php
$params = array(
'parameters' => array(
'DocType' => '01',
'UserMail' => 'email#email.email',
'FieldValues' => array(
'BaseDocumentField' => array(
'name' => 'something',
'value' => 'something',
),
'BaseDocumentField' => array(
'name' => 'something',
'value' => 'something',
),
),
)
);
$client = new SoapClient("http://servicename?wsdl", array("trace" => 1, "exceptions" => 0, "login" => $login, "password" => $password));
$result = $client->CreateDocument($params);
try {
$request = $client->CreateDocument($params);
$last_request = $client->__getLastRequest();
} catch (SoapFault $exception) {
$last_request = $client->__getLastRequest();
}
var_dump($last_request);
?>
The function __getLastRequest produces the following result:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://schemas.datacontract.org/2004/07/WSSC.V4.DMS.EKV.WssDocsService"
xmlns:ns2="http://tempuri.org/">
<SOAP-ENV:Body>
<ns2:CreateDocument>
<ns2:parameters>
<ns1:DocType>01</ns1:DocType>
<ns1:FieldValues>
<ns1:BaseDocumentField/>
</ns1:FieldValues>
<ns1:UserMail>email#email.email</ns1:UserMail>
</ns2:parameters>
</ns2:CreateDocument>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
I do not understand how to properly construct an array so that the section is displayed correctly.
how can I do it?
If you're working with a complex WSDL, you should definitively use a WSDL to php generator such as the PackageGenerator project. It'll generate all the classes required to construct the request then to receive the response. Using a good IDE such as Eclipse or PhpStorm, you'll have the full auto-completion wich will ease you the request construction without having to wonder how to do it.

PHP SOAP client not creating body

After over a half a day of trying and reading tutorials on creating a simple SOAP client, I am no closer to retrieving a request from API I attempting to work with.
WSDL: http://publicapi.ekmpowershop31.com/v1.1/publicapi.asmx?WSDL
I can make the request from SOAP UI with the following simple SOAP request:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pub="http://publicapi.ekmpowershop.com/">
<soapenv:Header/>
<soapenv:Body>
<pub:GetOrders>
<!--Optional:-->
<pub:GetOrdersRequest>
<!--Optional:-->
<pub:APIKey>myApiKey</pub:APIKey>
</pub:GetOrdersRequest>
</pub:GetOrders>
</soapenv:Body>
</soapenv:Envelope>
This above returns the expected data.
When it comes to translating the request into a PHP I have the following:
$wsdl = 'http://publicapi.ekmpowershop31.com/v1.1/publicapi.asmx?WSDL';
$trace = true;
$exceptions = false;
$debug = true;
$client = new SoapClient($wsdl,
array(
'trace' => $trace,
'exceptions' => $exceptions,
'debug' => $debug,
));
$param = array('GetOrdersRequest' => array(
'APIKey' => 'myApiKey'
)
);
$resp = $client->GetOrders();
print_r($param);
print_r($client->__getLastRequest());
print_r($client->__getLastResponse());
If put the $param into the GetOrders function then, it breaks and nothing happens.
Even if I use an array in the $client->GetOrders(array('someArry' => $param)) then response and request still always looks the same and looks like the body of the SOAP request is never created:
Request:
?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://publicapi.ekmpowershop.com/"><SOAP-ENV:Body><ns1:GetOrders/></SOAP-ENV:Body></SOAP-ENV:Envelope>
Response:
<?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:Body><GetOrdersResponse xmlns="http://publicapi.ekmpowershop.com/"><GetOrdersResult><Status>Failure</Status><Errors><string>Object reference not set to an instance of an object.</string></Errors><Date>2017-04-03T16:00:42.9457446+01:00</Date><TotalOrders>0</TotalOrders><TotalCost xsi:nil="true" /></GetOrdersResult></GetOrdersResponse></soap:Body></soap:Envelope>
If anyone can shed some light on what I am doing wrong here that would be real big help?
P.S My experience of SOAP in PHP is limited as I am used to SOAP in a java env. Thanks
You need to pass the parameters into the $client->GetOrders() call. Also the WSDL defines some required parameters, so a minimal example is something like:
$wsdl = 'http://publicapi.ekmpowershop31.com/v1.1/publicapi.asmx?WSDL';
$trace = true;
$exceptions = false;
$debug = true;
$client = new SoapClient($wsdl,
array(
'trace' => $trace,
'exceptions' => $exceptions,
'debug' => $debug,
));
$param = array(
'GetOrdersRequest' => array(
'APIKey' => 'dummy-key',
'CustomerID' => 1,
'ItemsPerPage' => 1,
'PageNumber' => 1,
)
);
$resp = $client->GetOrders($param);
print_r($param);
print_r($client->__getLastRequest());
print_r($client->__getLastResponse());
This gives the error response:
<Errors><string>Invalid character in a Base-64 string.</string></Errors>
which presumably is because my API key is invalid.

PHP, SOAPClient: Cannot find dispatch method

This is my WSDL XML: (Generated with SoapUI)
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://soap.test.com/">
<soapenv:Header/>
<soapenv:Body>
<ser:myMethod>
<user>
<id>?</id>
<name>?</name>
</user>
<code>?</code>
</ser:myMethod>
</soapenv:Body>
</soapenv:Envelope>
And my PHP code to consume the "myMethod" method:
$opts = array(
'location' => 'http://example.com/myServices?WSDL',
'uri' => 'http://soap.test.com'
);
$client = new SOAPClient(null, $opts);
$res = $client->__soapCall('myMethod', array(
"id" => "123",
"name" => "Sam"
));
var_dump($res);
And I get the Cannot find dispatch method for {http://soap.test.com} myMethod error.
I've tested the SOAP with SoupUI and it has responsed correctly.
What is wrong here?

Issue in passing parameters in soap call to wsdl in php

I having a wsdl file which look like in the below structure
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:not="http://notification.ws.api.com/">
<soapenv:Header/>
<soapenv:Body>
<not:getNotificationWS>
<!--Optional:-->
<notification>
<!--Zero or more repetitions:-->
<notificationList>
<!--Optional:-->
<email>?</email>
<!--Optional:-->
<phone>?</phone>
<userId>?</userId>
</notificationList>
</notification>
</not:getNotificationWS>
</soapenv:Body>
</soapenv:Envelope>
I am calling the above wsdl via soap in my php, below is the soapcall which i am trying to call the above wsdl.
$response = new SoapClient("http://127.0.0.1:8080/API/ws/TESTWS?wsdl");
try{
$soapstr = $response->__soapCall("getNotificationWS",array("parameters" => array( 'email' => "google#gmail.com",
'phone' => "1111111",
'userId' => "100"
)));
echo print_r($soapstr);
}catch (Exception $e){
echo $e->getMessage();
}
I am not getting any output or error. I i print my last request it return the below xml
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://notification.ws.api.easymed.com/">
<SOAP-ENV:Body>
<ns1:getNotificationWS />
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
How to send data inside the soap request in php suggest me.
Can't see your 'parameters' in the xml. You don't use notification neither notificationList
array ( 'notification' => array ( 'notificationList' => array ( 'phone' => 1111, 'userId' => 999 ) ) )

Categories