SoapClient creating XML - php

I need to create this xml:
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header>
<wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/04/secext">
<wsse:UsernameToken>
<wsse:Username>user</wsse:Username>
<wsse:Password>password</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<vb:getAirportInfo xmlns:vb="http://www.example.com/schema/2005/02/booking.xsd">
<airport>BNE</airport>
<airport>PPP</airport>
<airport>MEL</airport>
</vb:getAirportInfo>
</soapenv:Body>
</soapenv:Envelope>
I am new to using SoapClients and need some help doing this. How would I do it?

To successfuly use SOAP from PHP, you need two things:
The first is SoapClient and/or SoapServer classes bundled with PHP. They work fine, see http://php.net/manual/en/book.soap.php for details.
The second is WsdlDocument library. It generates WSDL description of your services, so other clients can easily use it. See http://code.google.com/p/wsdldocument/
Using SoapClient is very simple, once you initialize it, you will get object on which you can call methods as usual and it will forward these calls to server.
SoapServer is only about creating instance of you service and calling handle method.
None of this includes manual handling of XML you posted. It magically works on it's own (quite literally).

I figured it out. I needed to do two things.
First I needed to create the header section which had the security in it.
$soap_client = new SoapClient("airportinfo.wsdl", array("trace" => 1,"exceptions"=>0));
$header_part = '
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" SOAP-ENV:mustUnderstand="1">
<wsse:UsernameToken>
<wsse:Username>'.$username.'</wsse:Username>
<wsse:Password>'.$password.'</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
';
$soap_var_header = new SoapVar( $header_part, XSD_ANYXML, null, null, null );
$soap_header = new SoapHeader( 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'wsse', $soap_var_header, true );
$soap_client->__setSoapHeaders($soap_header);
Second I needed to create an array and pass it to the function that was the WSDL had. I got a list of these by using __getFunctions(). I then used this code to generate the last of the xml
$airports = array("AirportInfoRQ" => array("AirportCode" => "PPP", "AirportCode" => "BNE"));
$responce = $soap_client->AxisTransaction($airports);
This gave me slightly different xml I stated above but it was the correct xml to get the SoapClient working correctly

Related

PHP - Generating correct SOAP data for Salesforce API

Can anyone help point me in the correct direction to generate valid XML to use with the Salesforce API. I normally avoid SOAP like the plague but have no choice here and it seems like I'm at the bottom of a vertical learning curve.
I've got a full WSDL which as far as I'm aware should cover every part of the request, including all the appropriate namespaces and complex types, but I just can't generate a request that matches their example.
A subset of the example request looks like the following -
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://soap.sforce.com/schemas/class/WebsiteAPI" xmlns:web1="http://soap.sforce.com/schemas/class/WebsiteAPIUtils">
<soapenv:Header>
<web:SessionHeader>
<web:sessionId> SESSION_ID_FROM_LOGIN_METHOD </web:sessionId>
</web:SessionHeader>
</soapenv:Header>
<soapenv:Body>
<web:upsertLeadOrUpdateAccount>
<web:leadFromWebsite>
<!--Optional:-->
<web1:salesforceId></web1:salesforceId>
...
By calling the relevant method I can get an <ns1:upsertLeadOrUpdateAccount> request (not the same namespace id but that doesn't really matter), with an empty <leadFromWebsite> element. However, I just can't work out how to get the <salesforceId> sub element in there with the correct namespace. Everything I do either seems to do nothing, or adds an "xsi-type" attribute to the parent rather than using the namespace prefix.
The closest I've got is the following, which adds the namespace to the root element, but seems to create a weird looking xsi-type="ns1:web1" attribute, rather than just prefixing all the elements with web1.
I do have complexTypes for everything in the WSDL so I'm sure there should be a way for the SOAP library to handle all the heavy lifting for me. I really don't want to resort to just generating the whole lot by hand.
$data = new stdclass();
$data->salesforceId = '123';
$args = [
'leadFromWebsite' => new SoapVar($data, SOAP_ENC_OBJECT, 'web1', 'http://soap.sforce.com/schemas/class/WebsiteAPIUtils')
];
Output -
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://soap.sforce.com/schemas/class/WebsiteAPIUtils"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ns2="http://soap.sforce.com/schemas/class/WebsiteAPI">
<SOAP-ENV:Header>
<ns2:SessionHeader>
<ns2:sessionId>123</ns2:sessionId>
</ns2:SessionHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns2:upsertLeadOrUpdateAccount>
<ns2:leadFromWebsite xsi:type="ns1:web1">
<salesforceId>123</salesforceId>
</ns2:leadFromWebsite>
</ns2:upsertLeadOrUpdateAccount>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
I haven't tested this fully with the salesforce API yet, but I think I've managed to generate XML that looks correct - at least a lot closer than I was.
Seems my main issue was confusing type and node namespaces. I wanted to specify the namespace for the node, not the data type.
The following code generates xml that is much closer to the example requests -
$data = [];
$data[] = new SoapVar('123', XSD_STRING, null, null, 'salesforceId', 'http://soap.sforce.com/schemas/class/WebsiteAPIUtils');
$args = [
'leadFromWebsite' => new SoapVar($data, SOAP_ENC_OBJECT, null, null, 'leadFromWebsite', 'http://soap.sforce.com/schemas/class/WebsiteAPI')
];
$s->upsertLeadOrUpdateAccount($args);
XML -
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://soap.sforce.com/schemas/class/WebsiteAPIUtils"
xmlns:ns2="http://soap.sforce.com/schemas/class/WebsiteAPI">
<SOAP-ENV:Header>
<ns2:SessionHeader>
<ns2:sessionId>123</ns2:sessionId>
</ns2:SessionHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns2:upsertLeadOrUpdateAccount>
<ns2:leadFromWebsite>
<ns1:salesforceId>123</ns1:salesforceId>
</ns2:leadFromWebsite>
</ns2:upsertLeadOrUpdateAccount>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Adding SOAP Header to SOAP call in PHP

Using PHP 5.5.9, I'm trying to make a SOAP call and I cannot figure out how to add this header:
<SOAP-ENV:Header>
<wsa:Action>https://websvcs.adtrack.com/SmartLeadImport/Submit_Lead</wsa:Action>
<wsa:MessageID>urn:uuid:GUID</wsa:MessageID>
<wsa:ReplyTo>
<wsa:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:Address>
</wsa:ReplyTo>
<wsa:To>http://stage.adtrack.com/SmartLeadImport/SmartLeadImport.asmx?wsdl</wsa:To>
<wsse:Security SOAP-ENV:mustUnderstand="1">
<wsu:Timestamp wsu:Id="Timestamp-GUID">
<wsu:Created>2017-10-19T14:02:37Z</wsu:Created>
<wsu:Expires>2017-10-19T14:07:37Z</wsu:Expires>
</wsu:Timestamp>
<wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="SecurityToken-GUID">
<wsse:Username>USERNAME</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">***************</wsse:Password>
<wsse:Nonce>SOMENONSENSE</wsse:Nonce>
<wsu:Created>2017-10-18T14:02:37Z</wsu:Created>
</wsse:UsernameToken>
</wsse:Security>
</SOAP-ENV:Header>
What I'm having a real problem with is the nested namespaces. I've tried a couple different approaches.
Adding the XML to a SoapVar, then making that a header, but I can't figure out how to specify namespaces.
Trying to create SoapHeaders, but those came out looking odd, and again, I don't know how to make sure all the namespaces get accounted for.
Any help with this would be amazing. Thanks.

How to call Soap Client PHP with urn?

Hi i have this WSDL that, when i test in soapUI, it works well.
but i am having problems to call it from php soap client.
<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:urn="urn:uXXX">
<soapenv:Header/>
<soapenv:Body>
<urn:Pesquisar soapenv:encodingStyle=
"http://schemas.xmlsoap.org/soap/encoding/">
<active xsi:type="xsd:boolean">true</active>
</urn:Pesquisar>
</soapenv:Body>
</soapenv:Envelope>
i am trying in PHP something like this:
$client = new \SoapClient($mywsdl);
$headers[] = new \SoapHeader('urn:uXXX', 'urn');
$client->__setSoapHeaders($headers);
var_dump($client->Pesquisar(['parameters'=>['active' => true]]));
PS: This soap, doens have authentication, because i am inside a server that have access to the soap server.

SOAP + WSDL + PHP

I need a litte bit help.
I have the following two URLS:
WSDL: https://amsel.dpwn.net/abholportal/gw/lp/schema/1.0/var3ws.wsdl
Endpoint: https://amsel.dpwn.net/abholportal/gw/lp/SoapConnector
Now I want to send the server something like this:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:var="https://amsel.dpwn.net/abholportal/gw/lp/schema/1.0/var3bl">
<soapenv:Header>
<wsse:Security soapenv:mustUnderstand="1"
xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity- secext-1.0.xsd">
<wsse:UsernameToken>
<wsse:Username>ws_online_retoure</wsse:Username>
<wsse:Password
Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-tokenprofile1.0#
PasswordText">Anfang1!</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<var:BookLabelRequest
portalId="OnlineRetoure"
deliveryName="Spanien_Var3"
shipmentReference="ShipRef Nextt"
customerReference="1.Feld via Webservice"
labelFormat="PDF"
senderName1="Willi Webservice"
senderName2="via Webservice"
senderCareOfName="careOfName"
senderContactPhone="0800 123456"
senderStreet="Webservice Street"
senderStreetNumber="8080"
senderBoxNumber="12"
senderPostalCode="28010"
senderCity="Madrid"/>
</soapenv:Body>
</soapenv:Envelope>
How can I do this in PHP?
Thanks!
I had the similar problem as well due to sparse examples on how to use SOAP with PHP. Here are a couple of examples that may guide you to developing the SOAP interface.
http://bisqwit.iki.fi/story/howto/phpajaxsoap/#IsSoapTheBestWay
http://geekswithblogs.net/THines01/archive/2010/03/07/callws.aspx
http://jimmyzimmerman.com/2007/02/soap-server-with-php5-part-1.html
SOAP is clearly an overkill for many purposes in my opinion.
See NuSoap.
It simplifies the build of WebServices and clients.

PHP SOAP generating correct schema

Hi im having some trouble creating a schmea using PHP.
I need to generate something similar to :
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:loc="http://some-url">
<soapenv:Header>
<wsse:Security xmlns:wsse="http://some-url">
<wsse:UsernameToken wsu:Id="UsernameToken-837" xmlns:wsu="http://some-url">
<wsse:Username>account-username</wsse:Username>
<wsse:Password Type="http://some-url">account-password</wsse:Password>
<wsse:Nonce>NuehdiIAyh==</wsse:Nonce>
<wsu:Created>2009-05-07T21:41:49.765Z</wsu:Created>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<loc:getLocation>
<loc:address>some-address</loc:address>
<loc:maximumAge>
<metric>Second</metric>
<units>100</units>
</loc:maximumAge>
<loc:responseTime>
<metric>Second</metric>
<units>100</units>
</loc:responseTime>
<loc:tolerance>LowDelay</loc:tolerance>
</loc:getLocation>
</soapenv:Body>
</soapenv:Envelope>
I've had 0 luck though. I tried some examples using Soapvar,Soapparam, anyone have an idea how I can go by doing this?
NuSOAP is an excellent toolkit for SOAP. It's quite easy to use, and the support is very good.
http://sourceforge.net/project/shownotes.php?release_id=552239
There is an excellent documentation available on how to create SOAP Scheme's.

Categories