I'm using the native SOAP class in PHP 5, having changed from NuSOAP as the native class is faster (and NuSOAP development seems to have ceased). However the PHP 5 SOAP lacks the ability to generate WSDL.
Has anyone experience of generating WSDL in PHP? If so, please recommend your preferred method.
Thanks.
Stuart,
If you or anyone else is looking for a solution to this problem here's what I did.
First get this script: http://www.phpclasses.org/browse/download/zip/package/3509/name/php2wsdl-2009-05-15.zip
Then look at its example files. After that I just sliced it the way I needed because I'm using codeigniter:
function wsdl(){
error_reporting(0);
require_once(APPPATH."/libraries/WSDLCreator.php"); //Path to the library
$test = new WSDLCreator("Webservice", $this->site."/wsdl");
//$test->includeMethodsDocumentation(false);
$test->addFile(APPPATH."/controllers/gds.php");
$test->addURLToClass("GDS", $this->site);
$test->ignoreMethod(array("GDS"=>"GDS"));
$test->ignoreMethod(array("GDS"=>"accessCheck"));
$test->createWSDL();
$test->printWSDL(true); // print with headers
}
That it, your all done.
Btw, I'm using SoapServer and SoapClient in php5+
You can try these options:
- https://code.google.com/p/php-wsdl-creator/ (GPLv3)
- https://github.com/piotrooo/wsdl-creator/ (GPLv3)
- http://www.phpclasses.org/package/3509-PHP-Generate-WSDL-from-PHP-classes-code.html (BSD like)
The first one might be your best option if the licence fits your project.
Generating a WSDL on the fly is not something that happens very often - it would tend to raise a few questions about the stability of your service!
Zend Studio can generate a WSDL from a PHP class, and there are a few other similar tools.
If you do need to generate the WSDL dynamically, take a look at Zend Framework library: Zend_Soap_AutoDiscover
Zend_Soap_AutoDiscover is a good alternative to NuSOAP. But, you can also create the WSDL file from scratch which can be very complicated and error prone. To ease this process, you can use an IDE to generate the WSDL file for your PHP functions and pass it as a parameter to your PHP SoapServer class. Check out the complete tutorial on How to generate wsdl for php native soap class
Related
I am trying to parse this complex SOAP response:
But I get lost with the namespaces and children methods...any idea how to extract the highlighted data?
I tried this:
$xml->children( $ns['S'] )->Body->children( $ns['ns7'] )->Answer->children( $ns['ns3'] );
but it doesn't work
You should use a WSDL to PHP generator which will ease you the request construction and response handling. Indeed, you'll handle PHP object with getters/setters which is better in my point of view.
I can only suggest you to try the PackageGenerator project.
I am developing a PHP-powered application component which exports some data to an XML file which must follow a scheme defined by XSD file. I know how to validate the file manually, but it would be very handy if this could be done within unit tests.
Is there any library or framework available which can me help doing that?
One possibility would be to use DOMDocument::schemaValidate or DOMDocument::schemaValidateSource. Since either of those two methods returns a boolean value, you could then assert that the returned value is true.
This PHPUnit XSD validation lib defines a custom PHPUnit_Framework_Constraint uses DOMDocument::schemaValidate.
The advantage is that it gives a useful error when the validation fails, rather than just saying that 'false is not true'.
Usage
$constraint = new Jasny\PHPUnit\Constraint\XSDValidation("myschema.xsd");
$xml = $this->object->doSomething();
$this->assertThat($xml, $constraint);
Note: This is a shameless plug of my own open source library.
I would like to create a web service in PHP which can be consumed by different consumers (Web page, Android device, iOS device).
I come from a Microsoft background so am confortable in how I would do it in C# etc. Ideally I would like to be able to provide a REST service which can send JSON.
Can you let me know how I can achieve this in PHP?
Thanks
Tariq
I developed a class that is the PHP native SoapServer class' REST equivalent.
You just include the RestServer.php file and then use it as follows.
class Hello
{
public static function sayHello($name)
{
return "Hello, " . $name;
}
}
$rest = new RestServer(Hello);
$rest->handle();
Then you can make calls from another language like this:
http://myserver.com/path/to/api?method=sayHello&name=World
(Note that it doesn't matter what order the params are provided in the query string. Also, the param key names as well as the method name are case-insensitive.)
Get it here.
I would suggest you go for Yii it is worth of learning. You can easily establish it in this.
Web Service. Yii provides CWebService and CWebServiceAction to simplify the work of implementing Web service in a Web application. Web service relies on SOAP as its foundation layer of the communication protocol stack.
Easiest way in PHP is to use GET/POST as data-in and echo as data-out.
Here's a sample:
<?php if(empty($_GET['method'])) die('no method specified');
switch($_GET['method']){
case 'add': {
if(empty($_GET['a']) || empty($_GET['b'])) die("Please provide two numbers. ");
if(!is_numeric($_GET['a']) || !is_numeric($_GET['b'])) die("Those aren't numbers, please provide numbers. ");
die(''.($_GET['a']+$_GET['b']));
break;
}
}
Save this as test.php and go to http://localhost/test.php?method=add&a=2&b=3 (or wherever your webserver is) and it should say 5.
PHP does have native support for a SOAP server ( The SoapServer class manual shows it) and I've found it pretty simple to use.
Creating a REST style API is pretty easy if you use a framework. I don't want to get into a debate about which framework is better but CakePHP also supports output as XML and I'm pretty sure others will as well.
If you're coming from a Microsoft background just be careful about thinking about "datasets". They are a very specific Microsoft thing and have been a curse of mine in the past. It's probably not going to be an issue for you, but you may want to just see the differences between Microsoft and open implementations.
And of course PHP has a native json_encode() function.
You can check out this nice RESTful server written for Codeigniter, RESTful server.
It does support XML, JSON, etc. responses, so I think this is your library.
There is even a nice tutorial for this on the Tutsplus network -
Working with RESTful Services in CodeIgniter
You can also try PHP REST Data Services https://github.com/chaturadilan/PHP-Data-Services
You can use any existing PHP framework like CodeIgniter or Symfony or CakePHP to build the webservices.
You can also use plain PHP like disscussed in this example
I'm currently dealing with an archaic payment processor that makes connecting to their service as hard as possible (including a custom client SSL cert, with a password, plus basic HTTP Auth after that). Long story short, I can't use SoapClient to make the request, but I have been able to do it with cURL.
I now have the response in a string, can I use SoapClient to parse it? I'd rather not have to parse it manually as a regular XML, since I'd have to duplicate a lot of functionality, like throwing a sensible exception when finding a <SOAP:Fault>, for example.
No, you can't.
(just answering this for posterity. Based on the lack of evidence to the contrary, you apparently can't use SoapClient to parse a SOAP response you already have)
You can define context using context option of SoapClient to tell SoapClient to use SSL certificates etc. Context may be created using stream_context_create with lots of options
Let's for a second imagine you had called SoapClient::__doRequest() and it returned your XML SOAP response into a variable called $response.
<?php
//LOAD RESPONSE INTO SIMPLEXML
$xml = simplexml_load_string($response);
//REGISTER NAMESPACES
$xml->registerXPathNamespace('soap-env', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('somenamespace', 'http://www.somenamespace/schema/');
//...REGISTER OTHER NAMESPACES HERE...
//LOOP THROUGH AND GRAB DATA FROM A NAMESPACE
foreach($xml->xpath('//somenamespace:MessageHeader') as $header)
{
echo($header->xpath('//somenamespace:MyData'));
}
//...ETC...
?>
That is just some example/pseudo code (not tested and won't work as-is). My point is that you manually acquired the SOAP response so now all you have to do is parse it. SimpleXML is one solution you could use to do that.
Is there available any tool for PHP which can be used to generate code for consuming a web service based on its WSDL? Something comparable to clicking "Add Web Reference" in Visual Studio or the Eclipse plugin which does the same thing for Java.
In PHP 5 you can use SoapClient on the WSDL to call the web service functions. For example:
$client = new SoapClient("some.wsdl");
and $client is now an object which has class methods as defined in some.wsdl. So if there was a method called getTime in the WSDL then you would just call:
$result = $client->getTime();
And the result of that would (obviously) be in the $result variable. You can use the __getFunctions method to return a list of all the available methods.
I've had great success with wsdl2php. It will automatically create wrapper classes for all objects and methods used in your web service.
I have used NuSOAP in the past. I liked it because it is just a set of PHP files that you can include. There is nothing to install on the web server and no config options to change. It has WSDL support as well which is a bonus.
This article explains how you can use PHP SoapClient to call a api web service.
Say you were provided the following:
<x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/" xmlns:int="http://thesite.com/">
<x:Header/>
<x:Body>
<int:authenticateLogin>
<int:LoginId>12345</int:LoginId>
</int:authenticateLogin>
</x:Body>
</x:Envelope>
and
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<authenticateLoginResponse xmlns="http://thesite.com/">
<authenticateLoginResult>
<RequestStatus>true</RequestStatus>
<UserName>003p0000006XKX3AAO</UserName>
<BearerToken>Abcdef1234567890</BearerToken>
</authenticateLoginResult>
</authenticateLoginResponse>
</s:Body>
</s:Envelope>
Let's say that accessing http://thesite.com/ said that the WSDL address is:
http://thesite.com/PortalIntegratorService.svc?wsdl
$client = new SoapClient('http://thesite.com/PortalIntegratorService.svc?wsdl');
$result = $client->authenticateLogin(array('LoginId' => 12345));
if (!empty($result->authenticateLoginResult->RequestStatus)
&& !empty($result->authenticateLoginResult->UserName)) {
echo 'The username is: '.$result->authenticateLoginResult->UserName;
}
As you can see, the items specified in the XML are used in the PHP code though the LoginId value can be changed.
Well, those features are specific to a tool that you are using for development in those languages.
You wouldn't have those tools if (for example) you were using notepad to write code. So, maybe you should ask the question for the tool you are using.
For PHP: http://webservices.xml.com/pub/a/ws/2004/03/24/phpws.html
HI I got this from this site : http://forums.asp.net/t/887892.aspx?Consume+an+ASP+NET+Web+Service+with+PHP
The web service has method Add which takes two params:
<?php
$client = new SoapClient("http://localhost/csharp/web_service.asmx?wsdl");
print_r( $client->Add(array("a" => "5", "b" =>"2")));
?>