I'm tasked at using SOAP to pull in data through a WP Plugin for ease of use. I'm stuck at using the supplied auth details to get the correct data. I'm using SOAPUI to explore the request and see the possible methods/calls.
The flow should be using the FetchNewListingsIds which will give me a list of IDS which I can then put into an array object and then use that again for FetchListings with the supplied IDs - that will then give me all the details for each listing which I can then take and pull in to display
This is what I've written so far:
function property_feed_call(){
$url = "https://exdev.server.propctrl.com/v5.4/Basic/AgencyIntegration.svc?wsdl";
$user = "MindesExdevFeedUser";
$pass = "MindesExdevFeedUser";
$options = [
'cache_wsdl' => WSDL_CACHE_NONE,
'trace' => 1,
'stream_context' => stream_context_create(
[
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
]
]
)
];
// Set request params
$params = array(
"Username" => $user,
"Password" => $pass,
);
$client = new SoapClient($url, $options);
$session = $client->FetchNewListingIds($params);
// Invoke WS method (Function1) with the request params
$response = $client->__soapCall("FetchListings", array($params));
// Print WS response
echo '<pre>';
print_r($session);
echo '</pre>';
}
An example of how the request looks (FetchListings) when I open it up with SOAPUI is:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v5="http://server.propctrl.com/v5_4" xmlns:ais="http://server.propctrl.com/v5_4/AIS" xmlns:arr="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<soapenv:Header>
<v5:Credentials>
<!--Optional:-->
<v5:Password>?</v5:Password>
<!--Optional:-->
<v5:Username>?</v5:Username>
</v5:Credentials>
</soapenv:Header>
<soapenv:Body>
<ais:FetchListings>
<!--Optional:-->
<ais:mandateIds>
<!--Zero or more repetitions:-->
<arr:int>?</arr:int>
</ais:mandateIds>
</ais:FetchListings>
</soapenv:Body>
</soapenv:Envelope>
So you can see its asking for Credentials which I have just not sure how to pass it to my request. And then for a mandateIds which I can get from another method that also requires the same Credentials.
Related
I need to generate following XML Soap request in PHP:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<ns1:SomeRequest enddate="01-01-2018 00:00:00" authCode="exampleexample">
<ns1:status>STATUS1</ns1:status>
<ns1:status>STATUS2</ns1:status>
</ns1:SomeRequest>
</soapenv:Body>
</soapenv:Envelope>
I'm trying like that:
$this->client = new SoapClient($this->wsdlUrl, [
'trace' => true,
'exception' => true,
'cache_wsdl' => WSDL_CACHE_NONE,
]);
$parameters = [
'authCode' => 'exampleexample',
'enddate' => '01-01-2018 00:00:00',
'status' => ['STATUS1', 'STATUS2']
];
$response = $this->client->SomeSimpleMethod($parameters);
And receive Array to string conversion error.
When I'm trying pass status as string, like that:
$parameters = [
'authCode' => 'exampleexample',
'enddate' => '01-01-2018 00:00:00',
'status' => 'STATUS1'
];
Then everything works fine.
The best way is to use a WSDL to PHP generator as you won't wonder how to correctly construct the request. As it is not always straightforward to do so, using the generated classes to construct the request and to send it, you will easily send the request and handle the response using the OOP approach.
Try the PackageGenerator project which is best suited to generate a PHP SDK from any SOAP WSDL.
I am creating an SOAP client using PHP and having issues with consuming. When I test the request direct XML using soapui it responds fine and works but with PHP using SoapClient class it tells me the same credentials which I use in soapui are incorrect.
Not sure what I am missing here. My code below
Below is my XML
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pric="http://upshot.co.uk/pricing_request_ws">
<soapenv:Header/>
<soapenv:Body>
<pric:retrieveProductsPricing>
<username>apiuser</username>
<password>pword</password>
<postcode>EC2R 7HP</postcode>
</pric:retrieveProductsPricing>
</soapenv:Body>
</soapenv:Envelope>
Below is my PHP
$wsdl = "http://URL?wsdl";
$client = new SoapClient($wsdl, array('trace'=>1));
try
{
$options = array(
'soap_version'=>SOAP_1_2,
'exceptions'=>true,
'trace'=>1,
'cache_wsdl'=>WSDL_CACHE_NONE
);
$params = array(
'User name' => 'apiuser',
'Password' => 'pword',
'Postcode' => 'EC2R 7HP'
);
$response = $client->retrieveProductsPricing($params);
print_r($response);
}
catch(SoapFault $e)
{
print_r($e);
This is my first time configuring a soap client so I'm sure I have potentially made a mistake in this.
Have a look at the first code:
<username>apiuser</username>
<password>pword</password>
<postcode>EC2R 7HP</postcode>
You should use the same keys for the array
$params = array(
'username' => 'apiuser',
'password' => 'pword',
'postcode' => 'EC2R 7HP'
);
Useful examples
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.
You are my last hope!
I'm trying to implement DHL'S rating API to a website.
They send to me the sample code, I tried it to the demo environment with demo credentials and it worked fine.
Later they change the endpoint url and send to me the permanent credentials.
After that I can't make it to work. I'm getting the following error:
SOAP Fault!
FaultCode: WSDL FaultString: SOAP-ERROR: Parsing WSDL: Couldn't load
from 'https://wsbexpress.dhl.com:443/gbl/expressRateBook' : failed to
load external entity
My PHP code goes like this:
<?php
// The url of the service
$url='https://wsbexpress.dhl.com:443/gbl/expressRateBook';
// the soap operation which is called
$action='euExpressRateBook_providerServices_ShipmentHandlingServices_Binder_getRateRequest';
// the xml input of the service
$xmlrequest='<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:rat="http://scxgxtt.phx-dc.dhl.com/euExpressRateBook/RateMsgRequest">
<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 wsu:id="UsernameToken-5" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<!-- ####Please use your test user credentials here#### -->
<wsse:Username>*************</wsse:Username>
<!-- ####Please use your test user credentials here#### -->
<wsse:Password type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">*************</wsse:Password>
<wsse:Nonce encodingtype="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">eUYebYfsjztETJ4Urt8AJw==</wsse:Nonce>
<wsu:Created>2010-02-12T17:40:39.124Z</wsu:Created>
</wsse:UsernameToken>
</wsse:Security>
<soapenv:Header/>
<soapenv:Body>
<rat:RateRequest>
<RequestedShipment>
<DropOffType>REGULAR_PICKUP</DropOffType>
<Account>*********</Account>
<Currency>EUR</Currency>
<UnitOfMeasurement>SI</UnitOfMeasurement>
<Ship>
<Shipper>
<StreetLines>TEST SHIPPER</StreetLines>
<City>ALIMOS</City>
<PostalCode>174 55</PostalCode>
<CountryCode>GR</CountryCode>
</Shipper>
<Recipient>
<StreetLines>Main Street</StreetLines>
<City>GENEVA</City>
<PostalCode>1201</PostalCode>
<CountryCode>CH</CountryCode>
</Recipient>
</Ship>
<Packages>
<RequestedPackages number="1">
<Weight>
<Value>0.5</Value>
</Weight>
<Dimensions>
<Length>3</Length>
<Width>2</Width>
<Height>1</Height>
</Dimensions>
</RequestedPackages>
</Packages>
<ShipTimestamp>2015-12-28T00:01:00GMT+00:00</ShipTimestamp>
<Content>NON_DOCUMENTS</Content>
<PaymentInfo>DAP</PaymentInfo>
</RequestedShipment>
</rat:RateRequest>
</soapenv:Body>
</soapenv:Envelope>';
try {
// the soap client accepts options, we specify the soap version
// The trace option enables tracing of request so faults can be backtraced.
// The exceptions option is a boolean value defining whether soap errors throw exceptions of type SoapFault.
$options = array(
'soap_version'=>SOAP_1_1,
'exceptions'=>true,
'trace'=>1,
);
// create the soapclient and invoke __doRequest method
$client = new SoapClient($url, $options);
$output= $client->__doRequest($xmlrequest, $url, $action, 1);
}
catch (SoapFault $fault) {
echo "<h2>SOAP Fault!</h2><p>";
echo "FaultCode: {$fault->faultcode} <br/>";
echo "FaultString: {$fault->faultstring} <br/>";
echo("</p/>");
}
echo ("<h2>WSDL URL: </h2><p>");
echo ($url);
echo("</p/>");
echo ("<h2>Action: </h2><p>");
echo ($action);
echo("</p/>");
echo("<h2>XMLRequest: </h2><p>");
echo ($xmlrequest);
echo("</p/>");
if (!isset($output)) {
echo "<h2>SOAP Fault!</h2><p>";
echo "FaultCode: {$output->faultcode} <br/>";
echo "FaultString: {$output->faultstring} <br/>";
}else{
echo("<h2>Output: </h2><p>");
echo $output;
echo("</p/>");
}
?>
Where ********* my actual credentials.
If I give as endpoint url
https://wsbexpress.dhl.com/gbl/expressRateBook?wsdl
I'm getting as response the SOAP schema.
Could you help to find out whats wrong and I'm no getting the right response?
Try to experiment with options like this:
$opts = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$options = array (
'encoding' => 'UTF-8',
'verifypeer' => false,
'verifyhost' => false,
'soap_version' => SOAP_1_2,
'trace' => 1,
'exceptions' => 1,
'connection_timeout' => 180,
'stream_context' => stream_context_create($opts),
'cache_wsdl' => WSDL_CACHE_NONE,
//'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP | 1
);
I am trying to figure out how to use a SOAP interface.
I have managed to put together a code after exploring with soapUI.
Working soapUI request:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v22="https://shop.textalk.se/webservice/v22">
<soapenv:Header/>
<soapenv:Body>
<v22:GetOrders>
<Login>
<Shop>23946</Shop>
<Username>user#mysite.com</Username>
<Password>HIDDEN</Password>
</Login>
<!--Zero or more repetitions:-->
<Orders>44753668</Orders>
<Status>All</Status>
</v22:GetOrders>
</soapenv:Body>
</soapenv:Envelope>
Non working PHP request:
<?php
$wsdl = "https://shop.textalk.se/webservice/v22/?WSDL";
$client = new SoapClient($wsdl, array(
'Shop'=>'23946',
'Username'=>'user#mysite.com',
'Password'=>'HIDDEN',
'trace'=>1,
'exceptions'=>0));
$request = array(
'GetOrdersResponse' => array(
'Orders' => '44753668',
'Status' => 'All'
),
);
$response = $client->GetOrders($request);
var_dump($response);
echo $response;
?>
Documentation is here: https://shop.textalk.se/webservice/v22/wsdldoc.php
When I run the php code absolutely nothing happens
I am not a PHP expert but have you tried the below code?
<?php
$wsdl = "https://shop.textalk.se/webservice/v22/?WSDL";
$client = new SoapClient($wsdl);
$request = array(
'Shop'=>'23946',
'Username'=>'user#mysite.com',
'Password'=>'HIDDEN',
'Orders' => '44753668',
'Status' => 'All'));
$response = $client->GetOrders($request);
var_dump($response);
echo $response;
?>
Check out this blog post for a good starting point http://www.vankouteren.eu/blog/2009/03/simple-php-soap-example/
Note: the code is untested!
I found out what was wrong. I had to pass the login values as an array, and not as separate values. That solved it. I am now getting correct data in return.
<?php
$wsdl = "https://shop.textalk.se/webservice/v22/?WSDL";
$client = new SoapClient($wsdl);
$request = array(
'Login' => array(
'Shop'=>"23946",
'Username'=>"mail#mysite.com",
'Password'=>"HIDDEN"),
'Orders' => "44753668",
'Status' => "All");
$response = $client->GetOrders($request);
var_dump($response);
echo $response;
?>