I am new to the whole web service thing so I will try to explain my problem as much as I can understand it so far.
I created Soap server in PHP:
try {
$server = new SOAPServer(
'webservice.wsdl',
array(
'uri' => 'http://example.com/soap/server.php'
)
);
$server->setClass('soap');
$server->handle();
} catch (SOAPFault $f) {
print $f->faultstring;
}
Then I have a Soap class for the server:
class soap
{
public function sendOrders($sXml)
{
$oXml = new SimpleXMLElement($sXml);
$sOrder = $oXml->children();
$sResult = '';
foreach ($sOrder as $OrderRow) {
$sOrderNr = $OrderRow->ORDERNR;
$sState = $OrderRow->STATE;
$sTimestamp = $OrdeRow->TIMESTAMP;
$oOrder = new Order;
$oOrder->load($sOrderNr);
if ($sState == 1) {
$oOrder->checkStatusChange('Approved', $oOrder);
$oOrder->{$oOrder->getCoreTableName() . '__status'} = new Field('Approved');
$sResult .= $sOrderNr . " 1 | ";
} elseif ($sState == 0) {
$oOrder->checkStatusChange('Declined', $oOrder);
$oOrder->{$oOrder->getCoreTableName() . '__status'} = new Field('Declined');
$sResult .= $sOrderNr . " 0 | ";
}
$oOrder->save();
}
return $sResult;
}
}
I have a test client that sends simple XML with this format:
<xml>
<ORDER>
<ORDERNR>9nmf997d997701e15e30edac107b3664</ORDERNR>
<STATE>1</STATE>
<TIMESTAMP>123456789</TIMESTAMP>
</ORDER>
<ORDER>
<ORDERNR>9nmb1c3d4dfb067c04497ea51bd50d06</ORDERNR>
<STATE>0</STATE>
<TIMESTAMP>987654321</TIMESTAMP>
</ORDER>
</xml>
Now, what I need to do is create simple WSDL file for "description" of the service. I must say I have a little knowledge about this whole Web Service area so I would be grateful for any help.
I am familiar with W3C documentation http://www.w3schools.com/webservices/ws_wsdl_documents.asp
Thank you in advance.
Update: I tried to seach for some WSDL generators, none seem to be working.
Solved
A simple library called PhpWsdl did the trick.
https://code.google.com/p/php-wsdl-creator/
Related
I can't seem to find any code to display returned values from the call.
I am running the xml-lib from the software vendor at the following link
https://support.sippysoft.com/support/solutions/articles/3000013653-xml-rpc-api-sign-up-html-page-fresh-version-
<?php
include 'xmlrpc/xmlrpc.inc';
function listAccounts()
{
//$params = array(new xmlrpcval(array("i_account"=> new xmlrpcval('14719', "string")), 'struct'));
$msg = new xmlrpcmsg('listAccounts');
/* replace here URL and credentials to access to the API */
$cli = new xmlrpc_client('https://DOMAINHERE/xmlapi/xmlapi');
$cli->setSSLVerifyPeer(false);
$cli->setSSLVerifyHost(false);
$cli->setCredentials('USERNAME', 'PASSWORD', CURLAUTH_DIGEST);
$r = $cli->send($msg, 20);
if ($r->faultCode()) {
error_log("Fault. Code: " . $r->faultCode() . ", Reason: " . $r->faultString());
print_r ($r->faultString());
return false;
}
else
{
return $r->value();
// I need something here to write returned values to normal PHP variable
}
}
Ok thanks to the comment from halfer.
I managed to get the issue and digging through the code in the library I found a function that does the trick.
Thanks a million for your pointer it really helped.
I am new to php and xml and the learning curve is quite tall but thanks.
for someone else reference maybe in the future here is the corrected code with the last 2 lines that does the magic for me.
<?php
include 'xmlrpc/xmlrpc.inc';
// $params = array(new xmlrpcval(array("offset"=> new xmlrpcval("1", "int")
// ,"i_customer"=> new xmlrpcval("321", "int")
// ), 'struct'));
$params = array(new xmlrpcval(array("i_customer"=> new xmlrpcval("321", "int")
), 'struct'));
$msg = new xmlrpcmsg('listAccounts', $params);
/* replace here URL and credentials to access to the API */
$cli = new xmlrpc_client('DOMAIN');
$cli->setSSLVerifyPeer(false);
$cli->setdebug(0);
$r = $cli->send($msg, 20); /* 20 seconds timeout */
if ($r->faultCode()) {
error_log("Fault. Code: " . $r->faultCode() . ", Reason: " . $r->faultString());
echo $r->faultString();
}
// now lets decode the xml response..
$values=php_xmlrpc_decode($r->value());
var_dump ($values['accounts'][0][username]);
?>
This is my first run into the SOAP forest, so I have no idea what I'm doing and worse, the company has zero documentation or code examples. They at least provide an example call.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:GetRatesIPXML>
<tem:ipXML>
<![CDATA[
<XML>
<RateInput>
<GUID>12345</GUID>
<RepID>abc</RepID>
<ZipCode>55343</ZipCode>
<EffectiveDate>1/15/2014</EffectiveDate>
<DateOfBirth >12/15/1980</DateOfBirth >
<FilterPlanCode></FilterPlanCode>
</RateInput>
</XML>]]>
</tem:ipXML>
</tem:GetRatesIPXML>
</soapenv:Body>
</soapenv:Envelope>
I've used both SoapClient and NuSoap. I've tried everything from nested arrays to objects, strings, simpleXML. I can't seem to figure this out and after two days of googling I've reached my end.
Here's my current implementation.
require('lib/nusoap.php');
class Carrier
{
const WSDL = 'http://getrates_staging.test.com/getrates.svc?wsdl';
public function get()
{
$soapClient = new nusoap_client( self::WSDL , true);
$soapClient->soap_defencoding = 'UTF-8';
$string = ""
. "<XML>"
. "<RateInput>";
$string .= "<GUID>12345</GUID>";
$string .= "</RateInput></XML>";
$response = $soapClient->call('GetRatesIPXML' , array('ipXML'=> $string) , '' , '', false, true);
var_dump($soapClient->request);
var_dump($soapClient->getError());
var_dump($response);
}
}
$foo = new Carrier();
$foo->get();
It results in something close but all the < get escaped to < so that doesn't work. Any help is appreciated.
edit
This is about as close as I get to the desired result
class Carrier
{
const WSDL = 'http://getrates_staging.test.com/getrates.svc?wsdl';
public function get()
{
$soapClient = new SoapClient( self::WSDL , array('trace' => true));
//$soapClient->soap_defencoding = 'UTF-8';
$string = ""
. "<![CDATA[ <XML>"
. "<RateInput>";
$string .= "<GUID>12345</GUID>";
$string .= "</RateInput></XML> ]]>";
$param = new SoapVar($string, XSD_ANYXML);
$ipXML = new stdClass();
$ipXML->ipXML = $param;
try
{
$response = $soapClient->GetRatesIPXML($ipXML);
}
catch(Exception $e)
{
var_dump($e);
}
var_dump($soapClient->__getLastRequest());
var_dump($response);
}
}
$foo = new Carrier();
$foo->get();
I end up with
<?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:GetRatesIPXML><![CDATA[ <XML><RateInput><GUID>12345</GUID></RateInput></XML> ]]></ns1:GetRatesIPXML></SOAP-ENV:Body></SOAP-ENV:Envelope>
I don't get why it's dropping the surrounding <ns1:ipXML>
edit 2
At the end of the day this worked.
class Carrier
{
const WSDL = 'http://getrates_staging.test.com/getrates.svc?wsdl';
public function get()
{
$soapClient = new SoapClient( self::WSDL , array('trace' => true));
//$soapClient->soap_defencoding = 'UTF-8';
$string = ""
. "<ns1:ipXML><![CDATA[ <XML>"
. "<RateInput>";
$string .= "<GUID>1234</GUID>
<RepID>1234</RepID>
<ZipCode>55343</ZipCode>
<EffectiveDate>1/15/2016</EffectiveDate>
<DateOfBirth >07/01/1983</DateOfBirth >
<FilterPlanCode></FilterPlanCode>";
$string .= "</RateInput></XML> ]]></ns1:ipXML>";
$param = new SoapVar($string, XSD_ANYXML);
$ipXML = new stdClass();
$ipXML->ipXML = $param;
try
{
$response = $soapClient->GetRatesIPXML($ipXML);
}
catch(Exception $e)
{
var_dump($e);
}
var_dump($soapClient->__getLastRequest());
var_dump($response);
}
}
$foo = new Carrier();
$foo->get();
But it seems so hacky. If anyone has a better suggestion I'm open.
Ordinarily, XML documents are constructed (and parsed) using PHP "helper libraries" such as SimpleXML (http://php.net/manual/en/book.simplexml.php), or techniques such as the ones described here (http://www.phpeveryday.com/articles/PHP-XML-Tutorial-P848.html).
The XML document is constructed as an in-memory data structure (arrays, etc.), and then converted, in one swoop, into the XML that is to be sent.
In fact, since what you are doing is SOAP, you can go one level of abstraction above that, e.g. (http://php.net/manual/en/book.soap.php). Off-the-shelf libraries exist which will handle both the task of constructing the XML payload, and sending it, and getting server responses and decoding them. That's where you should start.
"Actum Ne Agas: Do Not Do A Thing Already Done."
I have generated some XML which is saved to a file.
Dummy_Order.xml
In the following code I wanted to open the XML and use it in the method 'ImpOrders'
Here is my code
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$proxy = new SoapClient('http://soapclient/wsdl/Web?wsdl', array ('trace' => 1));
if (file_exists('Dummy_Order.xml')) {
$xml = file_get_contents('Dummy_Order.xml');
} else {
exit('Failed to open XML.');
}
$xmlstring = new SimpleXMLElement($xml);
$result = $proxy->ImportOrders($xmlstring);
var_dump($result);
echo "REQUEST:\n" . $proxy->__getLastRequest() . "\n";
?>
I am getting a response in $result of 0 imported 0 skipped. So i then did getLastRequest() and it's adding the code from the Method but not adding my XML. It wants my XML in a string - which it current is in and isnt moaning about that (It does moan if i use it ->asXML).
I have tried
$result = $proxy->ImportOrders();
and
$result = $proxy->ImportOrders($xmlstring);
And both show the same result in _getLastRequest, which led me to believe that my string isn't being plugged in.
When I check the functions using _getFunctions it provides the information of this...
ImportResult ImportOrders(string $Orders)
Any help would be awesome!
Ok so i resolved it. All i needed to do was wrap my answer in <<
Like below.
$teststring = <<<XML
$xml
XML;
$result = $proxy->ImportOrders($teststring);
Hope this helps out anyone else using PHP, SoapClient and XML.
This is my Soap Response xml,I need to get RebillCustomerID
How I can do this?
<?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:Header><eWAYHeader
xmlns="http://www.eway.com.au/gateway/rebill/manageRebill">
<eWAYCustomerID>87654321</eWAYCustomerID><Username>test#eway.com.au</Username>
<Password>test123</Password></eWAYHeader></soap:Header><soap:Body>
<CreateRebillCustomerResponse xmlns="http://www.eway.com.au/gateway/rebill/manageRebill"><CreateRebillCustomerResult>
<Result>Success</Result><ErrorSeverity /><ErrorDetails />
<RebillCustomerID>90246</RebillCustomerID></CreateRebillCustomerResult>
</CreateRebillCustomerResponse></soap:Body></soap:Envelope><pre></pre>
try the code below. I am assuming your xml response is in $soapXMLResult variable
$soap = simplexml_load_string($soapXMLResult);
$response = $soap->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children()->CreateRebillCustomerResponse;
$customerId = (string) $response->CreateRebillCustomerResult->RebillCustomerID;
echo $customerId;
How I did, create eway soap request and get eway soap result,may help others!
<?php
$URL = "https://www.eway.com.au/gateway/rebill/test/manageRebill_test.asmx?wsdl";
$option = array("trace"=>true);
$client = new SOAPClient($URL, $option);
$functions = $client->__getFunctions();
$headeroptions=array('eWAYCustomerID'=>"87654321",'Username'=>"test#eway.com.au","Password"=>"test123");
$header = new SOAPHeader('http://www.eway.com.au/gateway/rebill/manageRebill', 'eWAYHeader',$headeroptions);
$bodyoptions = array(
"CreateRebillCustomer" => array(
"customerTitle" => "Mr",
"customerFirstName"=>"Muhammad",
"customerLastName"=>"Shahzad",
"customerAddress"=>"cust ome rAddress",
"customerSuburb"=>"customer Suburb",
"customerState"=>"ACT",
"customerCompany"=>"customer Company",
"customerPostCode"=>"2345",
"customerCountry"=>">Australia",
"customerEmail"=>"test#gamil.com",
"customerFax"=>"0298989898",
"customerPhone1"=>"0297979797",
"customerPhone2"=>"0297979797",
"customerRef"=>"Ref123",
"customerJobDesc"=>"customerJobDesc",
"customerComments"=>"customerComments",
"customerURL" => "http://www.acme.com.au"
)
);
try{
$response = $client->__soapCall("CreateRebillCustomer", $bodyoptions,NULL,$header,$outputHeader);
//echo $client->__getLastRequest();
//$response = $client->CreateRebillCustomer($bodyoptions);
//echo "<pre>";echo "<br/>";
// print_r($response);
echo $result = $response->CreateRebillCustomerResult->Result;echo "<br/>";
echo $customerId = $response->CreateRebillCustomerResult->RebillCustomerID;echo "<br/>";
echo "<br/>";
if($result=='Success' AND $customerId){
echo 'Member Created at eWay Successfully!...<br/>';
echo 'Creating Recurring Billing Cycle on eWay,Please wait......<br/>';
//$UpdateRebillCustomer = CreateRebillEvent($customerId);
//print_r($UpdateRebillCustomer);
}
else{
echo $ErrorSeverity = $response->CreateRebillCustomerResult->ErrorSeverity;echo "<br/>";
echo $ErrorDetails = $response->CreateRebillCustomerResult->ErrorDetails;echo "<br/>";
}
}
catch(SOAPFault $e){
print $e;
}
if($customerId){
$bodyoptions2 = array(
"CreateRebillEvent " => array(
"RebillCustomerID" => $customerId,
"RebillInvRef" => "Ref123",
"RebillInvDes"=>"description",
"RebillCCName"=>"Mr Andy Person",
"RebillCCNumber"=>"4444333322221111",
"RebillCCExpMonth"=>"12",
"RebillCCExpYear"=>"15",
"RebillInitAmt"=>"100",
"RebillInitDate"=>date('d/m/Y'),
"RebillRecurAmt"=>"200",
"RebillStartDate"=>date('d/m/Y'),
"RebillInterval"=>"31",
"RebillIntervalType"=>"1",
"RebillEndDate"=>"31/12/2013",
)
);
try{
$response = $client->__soapCall("CreateRebillEvent", $bodyoptions2,NULL,$header,$outputHeader);
//echo $client->__getLastRequest();
//print_r($response);
echo "<br/>";
echo $result2 = $response->CreateRebillEventResult->Result;echo "<br/>";
echo $RebillCustomerID = $response->CreateRebillEventResult->RebillCustomerID;echo "<br/>";
if($result2=='Success'){
echo 'Recurring Cycle Created Successfully at eWay!...<br/>';
echo 'Member Id is ===>'.$RebillCustomerID;
//$UpdateRebillCustomer = CreateRebillEvent($customerId);
//print_r($UpdateRebillCustomer);
}
else{
echo $ErrorSeverity = $response->CreateRebillEventResult->ErrorSeverity;echo "<br/>";
echo $ErrorDetails = $response->CreateRebillEventResult->ErrorDetails;echo "<br/>";
}
}
catch(SOAPFault $e){
print $e;
}
}
?>
If you are writing a client for a SOAP service and you want to manipulate or test their response by placing it in a local file and parsing that (maybe their response has errors and you want to fix the errors yourself), you can override the URL that your soapClient uses to call methods by calling __soapCall() directly:
$result = $this->soap_client->__soapCall( 'function_call_name', ['location' => $file_location] );
Where $file_location is the absolute or relative path to your copy of their xml response. What you put as your function call name doesn't matter, since the soap client will get the same xml response regardless of what function you try to call this way. The only thing that matters is that your $file_location be correct and that the xml file has the correct permissions to be read by apache if you run this in a browser.
You can then var_dump($response) and find the node you need rather than using simplexml_load_string. If you do it this way you can easily swap out your xml for their response later on.
I am new to Web Services and am struggling to access/read the XML data using PHP (my website that will be using the data is in PHP).
The WSDL Url: http://services.mywheels.co.za/BWAVehicleStockService.svc?wsdl
I need to get access and read the Vehicle stock information but cant see to access anything.
the Array vehicle are stored under: http://services.mywheels.co.za/BWAVehicleStockService.svc?xsd=xsd2 .
i am using this code but it doesnt give my any data. I also have a GUID that i need to pass but have no idea how to add it to the header.
<?PHP
define('NEWLINE', "<br />\n");
// SOAP client
$wsdl = 'http://services.mywheels.co.za/BWAVehicleStockService.svc?wsdl';
$soapClient = new SoapClient($wsdl, array('cache_wsdl' => 0));
// SOAP call
$parameters->ArrayOfVehicle->Vehicle;
try
{
$result = $soapClient->GetVehicleStock($parameters);
}
catch (SoapFault $fault)
{
echo "Fault code: {$fault->faultcode}" . NEWLINE;
echo "Fault string: {$fault->faultstring}" . NEWLINE;
if ($soapClient != null)
{
$soapClient = null;
}
exit();
}
$soapClient = null;
echo "<pre>\n";
print_r($result);
echo "</pre>\n";
echo "Return value: {$result->GetDataResult}" . NEWLINE;
?>
if someone can help or point me in the right direction with this that would be great.
Thanks
You can add headers using __setSoapHeaders():
$h = new SoapHeader('http://tempuri.org/', 'Guid', '123');
$soapClient->__setSoapHeaders($h);
I had to read the WSDL itself to find out what namespace I should use; in this case they refer to Guid as tns:Guid and from the top you can read what URI is used to express that, hence http://tempuri.org.