I am trying to create a web service with PHP. The following is my code -
Web Server -
require 'inventory_functions.php';
ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache
$server = new SoapServer("inventory.wsdl");
$server->addFunction("getItemCount");
$server->handle();
Inventory_functions.php -
function getItemCount($upc){
//in reality, this data would be coming from a database
$items = array('12345'=>5,'19283'=>100,'23489'=>'234');
return $items[$upc];
}
My Client Test -
ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache
$client = new SoapClient("http://www.xxx.co.uk/service/inventory.wsdl");
$return = $client->getItemName('12345');
print_r($return);
When I run this everythign is OK. the number "5" will output in my browser. WhatI really need is some help in how to go about sending data via XML to the SOAP server, from their I will add this data to MySQL.
How would I send the XML vie the client test?
Thanks
I'm not sure I understand your question. You want to know what XML input you should give your web-service in order to send for instance the value "5"?
In order to do that you should first analyse the wsdl file that is generated, then, depending on your programming language of choice for the client, you may generate a client Stub to interact with the Web-Service itself.
Alternatively, you may issue an HTTP POST with the XML directly to the web-service (should look something like this):
POST /service/mywebservice.php HTTP/1.1
Host: www.xxx.co.uk
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "<your.webservice.namespace>/getItemCount"
<?xml version="1.0" encoding="utf-8"?>
<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:Body>
<getItemCount xmlns="<your.webservice.namespace>">
<value>5</value>
</getItemCount>
</soap:Body>
</soap:Envelope>
Related
For example I have found this free web service:
http://www.webservicex.net/ConvertTemperature.asmx
and I want to test my client, but I don't know what input parameters I should specify for my request.
This happens with a number of APIs that I want to test with. Is there any way to find out what possible values could be, since there is no documentation?
You've got several options here.
If you've installed Visual Studio you can use WCF Test Client to test the API.
More information on WCF Test Client HERE
Alternatively you can use SoapUI by SmartBear.
Most of the time you just specify the URL of the service and it will give you a list of all the available methods and what inputs they expect.
Actually, by the link you've submitted, there is a document with the parameters explained: http://www.webservicex.net/ConvertTemperature.asmx?op=ConvertTemp
Request:
POST /ConvertTemperature.asmx HTTP/1.1
Host: www.webservicex.net
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.webserviceX.NET/ConvertTemp"
<?xml version="1.0" encoding="utf-8"?>
<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:Body>
<ConvertTemp xmlns="http://www.webserviceX.NET/">
<Temperature>double</Temperature>
<FromUnit>degreeCelsius or degreeFahrenheit or degreeRankine or degreeReaumur or kelvin</FromUnit>
<ToUnit>degreeCelsius or degreeFahrenheit or degreeRankine or degreeReaumur or kelvin</ToUnit>
</ConvertTemp>
</soap:Body>
</soap:Envelope>
Temperature, FromUnit and ToUnit are the request/input parameters.
Considering the server is asmx-based, this URL should give you the full schema: http://www.webservicex.net/ConvertTemperature.asmx?wsdl
Hope I understood your question correctly.
P.S. It's not usually a good idea to test your code (e.g. auto-test) against a live (or production) system.
I am currently integrating with Kashflow and using SOAP calls to send Customer data to my Kashflow account from my Symfony2 site, and it should return the ID of the new customer - I need this in order to save it in the database at my end.
However, upon return, I get the HTTP header and not just the ID, which means it is not inserting it into the database as it needs to be an integer.
Here is the SOAP response:
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<InsertCustomerResponse xmlns="KashFlow">
<InsertCustomerResult>int</InsertCustomerResult>
<Status>string</Status>
<StatusDetail>string</StatusDetail>
</InsertCustomerResponse>
</soap12:Body>
</soap12:Envelope>
I am trying to retrieve the new ID by using:
return new Response((int)$response->soapBody->InsertCustomerResponse->InsertCustomerResult);
But even though the ID does get returned, it spits out the HTTP header above it. Is there any way of removing this? I have tried using preg_replace like this (where $return is the response from the SOAP call):
preg_replace('/((.*)\n)*(\d+)$/','$1',$return);
But it just returns a blank.
Any ideas?
Thanks in advance
I realised that I have to use getContent() on the return in order to get the data I needed.
So:
$response = $response->soapBody->InsertCustomerResponse->InsertCustomerResult;
return $response->getContent();
Which then only gets the data I need, and not the header as well.
I am using PHP to connect to a Web Service.
I need to connect to the web service with some login details so I can generate a Ticket to start using the methods available.
Here is some code:
//Connect To WebCrm API
$client = new SoapClient("http://b2b-email.net/apicrm1/webCRMAPI.asmx?wsdl", array('trace' => 1));
//Login
$ticket = $client->Authenticate(array('code' => 'rhgkhgk','user' =>'myusername','password' =>'apass'));
From this in the response soap header a ticket will be generated. This is generated under Ticket Header Then GUID. (See Below)
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Header>
<TicketHeader xmlns="http://www.webcrm.com/">
<Guid>TICKET->>>>>>>>e446373e-8fg0-4dfc-b876-41f3bc8990dd</Guid>
</TicketHeader>
</soap:Header>
<soap:Body>
<AuthenticateResponse xmlns="http://www.webcrm.com/">
<AuthenticateResult>
<Message />
<Code>0</Code>
</AuthenticateResult>
</AuthenticateResponse>
</soap:Body>
</soap:Envelope>
I need this ticket ID to perform any other tasks using the web service but how can access it and use it within my code?
I have tried using below:
$response = $client->__getLastResponse();
However this outputs like below:
6d5933d3-46ff-4690-893d-2af04806668c->>>>>>>>0<<<<<ZERO ON THE END
A zero is always on the end when it shouldn't be?
Any help on why this is happening on the best way i can achieve accessing the ticket from Soap Header is greatly appreciated!
As per the manual:
$soapclient->__soapCall("soapmethod", array(parameters), null, $input_headers, &$output_headers);
$output_headers should then contain the headers from the response message.
$client->__getLastResponse() returns the XML of the last response. You are viewing this in your browser, and your browser is trying to interpret this as HTML. Because of this, it will not show any XML tags and only show text. That is why the 0 is displayed. You can view the whole XML in several ways:
View the source of the PHP page
Wrap the echo statement in <xmp></xmp> tags.
Call htmlentities() on the XML before echoing it.
I'm a complete noob in XML and SOAP,
Could you give some advice on at least where to start, or some example?
(I'm not begging to write code for me)
Here are the specs:
I just expect to receive two double values. What is an easiest way to do it?
POST /CurrencyConvertor.asmx HTTP/1.1
Host: www.webservicex.net
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<ConversionRate xmlns="http://www.webserviceX.NET/">
<FromCurrency>PHP</FromCurrency>
<ToCurrency>USD or EUR</ToCurrency>
</ConversionRate>
</soap12:Body>
</soap12:Envelope>
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<ConversionRateResponse xmlns="http://www.webserviceX.NET/">
<ConversionRateResult>double</ConversionRateResult>
</ConversionRateResponse>
</soap12:Body>
</soap12:Envelope>
The above specs's origin: http://www.webservicex.net/CurrencyConvertor.asmx?op=ConversionRate
The first block is supposed to be a request, and the other response...
EDIT
Alright, I stopped at a standard PHP class, but I don't quite understand what is being asked for in this __doRequest method:
$client = new SoapClient();
$client->__doRequest ( <<<EOD
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<ConversionRate xmlns="http://www.webserviceX.NET/">
<FromCurrency>PHP</FromCurrency>
<ToCurrency>USD or EUR</ToCurrency>
</ConversionRate>
</soap12:Body>
</soap12:Envelope>
EOD
, "http://www.webservicex.net/CurrencyConvertor.asmx" , $???, $??? );
http://www.php.net/manual/en/soapclient.dorequest.php
What is action, and what should I put as version, I know - a soap version 1.2 but the parameter is int so it cannot be assigned a 1.2 value lol...
EDIT2:
Alright, this is what I've got so far, but it gets me an empty string...
$client = new SoapClient(null, array('location'=>'http://www.webservicex.net/CurrencyConvertor.asmx','uri'=>''));
$client->__doRequest ( <<<EOD
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<ConversionRate xmlns="http://www.webserviceX.NET/">
<FromCurrency>PHP</FromCurrency>
<ToCurrency>USD or EUR</ToCurrency>
</ConversionRate>
</soap12:Body>
</soap12:Envelope>
EOD
, "http://www.webservicex.net/CurrencyConvertor.asmx" , "ConversionRate", 2 );
echo "Response :<br>", htmlentities($client->__getLastResponse());
...I just dont get this 'uri' thing - beyond my understanding. It makes me want to hit my head to the wall.
Basically what you need are two parts.
A SOAPClient class, which solves the communication, knows about the server URL, sends requests and receives responses (which you got in XML in your question) and also triggers #2, which is:
The second part is an XML parser / marshaller, which can convert a request objects (containing fromCurrency and toCurrency) to a correct XML string the server can understand (according to the WSDL) and convert some XML into a response object again using the WSDL. This part is a bit tricky, but I found a lot of documentation for that, search for "SOAP PHP" and you get some examples). These two processes are called marshalling and unmarshalling (to help you find something quicker with Google). Your StockQuote webservice doesn't really provide a WSDL, which is basically a description of all possible operations (in your case just one: GetQuote) and its available objects (in your case only simple types string, which don't have to be defined, since they are WSDL standard)
I haven't done SOAP with PHP, but spent a lot of time with Java+SOAP and can understand how difficult it is. In theory you just send some XML to the server and get some XML back. But the XML sent has to be in the correct format and when you receive a response, you want to convert the response into an object and not deal with some XML string.
For a simple web service like this, you might consider constructing the XML request by hand (simply putting together the XML string) and substr the response to find the requested answer.
I'm using Zend_Soap_Client object for sending a soap request to another application here is the format of the XML that it's sending to the server:
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="urllocation" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:enc="http://www.w3.org/2003/05/soap-encoding"><env:Body><ns1:isAccountActive env:encodingStyle="http://www.w3.org/2003/05/soap-encoding"><param0 xsi:type="xsd:string">thisisatest</param0></ns1:isAccountActive></env:Body></env:Envelope>
I'm using it on the other SOAP servers that I have and seems to work fine but one of the server returned a response "Invalid XML" that is why I'm really wondering why it won't work on that server alone. Any ideas would be greatly appreciated.
Additional Details:
I've tried to commentout the code that calls the method from the Server here is the code:
$client = new Zend_Soap_Client(null,
array(
'uri'=>'http://'.$user->customconfigs['alumniuri'],
'encoding'=>'UTF-8',
'location'=>'http://'.$user->customconfigs['alumnilocation']
)
);
echo "Location: {$user->customconfigs['alumnilocation']} - uri: {$user->customconfigs['alumniuri']}";
$alumniactive = $client->isAccountActive($token);
upon commenting out:
$alumniactive = $client->isAccountActive($token);
the error disappeared.This is the same codes from my other applications and it's working fine from there.
After a long search for the answer to this question I finally found the problem... this code is actually located on a joomla component which I was using the uri same as what I have from the location that would include a character "&" which is illegal on xml standards removing these character from my xml will then cause the SOAP server to accept the request as valid. :)