Link to home
Start Free TrialLog in
Avatar of zorawar_bahadur
zorawar_bahadur

asked on

Building web services in PHP

Hi,

I have just been assigned a project by my boss in which i have to design web services for our client installations.

The client product installation, an anti virus software, will invoke web services on our web servers and and the web services will respond back.

The web services have to be deployed on Linux machines.

My first recommendation to my boss was Java as Sun has lots of support available for Web services.

But he is reluctant to use Java for this project  as all our production systems and our web servers use PHP.

According to him, the use of Java would be introducing a new language in the already huge pool of programming languages being used in the company.

When i had a look at the support PHP has for web services i was not very impressed.

I couldnt find any good resource for making web services in PHP. Although i did find material for making web ser vices clients in PHP.

I need advice to advise my boss plus if there is a good resource on making web services in PHP, i would lilke to know about it.

What PHP modules are available for making PHP web services?

How easy it is to make them as compared to Java and .NET?

PS: I would be making web services and not the clients for web services. the clients are anti virus software made in C++.

Regards



Avatar of soapergem
soapergem
Flag of United States of America image

Can you be more specific about what "web services" you're talking about? As far as the comparison with the other languages, I'm a huge proponent of PHP; it's just as powerful as any other high-level language, and it never makes browsers crash (like Java :P). It has a huge library of functions: everything from your basic looping, comparisons, and output; to regular expressions, to reading/writing files, creating images on the fly, managing database information--you name it, PHP supports it. So please describe the "web services" you are speaking of.
Avatar of zorawar_bahadur
zorawar_bahadur

ASKER

Hi,

thanks for replying.

When i use the word "web services" i dont mean providing a web service. I mean the paradigm of "web services".

To make things a little clear, web services are open standard (XML, SOAP, etc.) based Web applications that interact with other web applications for the purpose of exchanging data. Initially used for the exchange of data on large private enterprise networks, web services are evolving to include transactions over the public Internet.

From a programmer's point of view, web services are just another way of doing RPC. but its pretty easy( atleast in java and .Net) and uses XML( SOAP to be in particular). In .net thats how you make a web service.

Function1()
{
... ...
......
.....
.....}

To make the above function globally available to other programmers/applications/ web sites all you have to do is to add the tag

web method on top.

web method

function1()
{
....
....
....
....
}

thats it. now your function can be invoked by any one from any where in any language on any platform.

a good real life example is the google web services.

http://www.google.com/apis/

You can use the methods/functions/procedures developed by Google to use in your own web applications.


ok now here is what i want to do.

Basically our products, anti virus software contact our web servers regularly and do different stuff like sending info and getting updates.

till now it has been a messy code written in PHP.

my boss wants to streamline every thing and provide a unified interface through web serivces which later on other anti virus companies can also use in their products.

just lilke google does. google provides searching facilities through its APIs. our company wants to provide PC security information through the web services APIs . we would also like to provide latest anti virus updates through this method.

lilke a web service called  updateVirusInfo( ) can be invoked by the anti virus software to update it self.


Now coming to the main question.

Does PHP support the paradigm "web services". does it has any packages like Perl has a package called SOAP::lite to  make web services in Perl.

if Yes how good is it as compared to the Sun JWSDP  http://java.sun.com/webservices/downloads/webservicespack.html

Do not confuse web services with the normal english word web services which would mean providing a service over the web like making a web site.

Sorry--my fault for not realizing what you were saying. Yes PHP supports SOAP, but I believe you need PHP version 5 for this. Unfortunately, this sort of thing really isn't my forte, so I'm going to back off and let some more qualified experts answer. But I do have some links for you, if that's any help at all.

http://www.php.net/soap
http://www.zend.com/php5/articles/php5-SOAP.php
thanks
Hi zoawar,

php is a pain in the ass for webservices in the sense that you have to do alot of the work yourself like regestering your "complex types" etc. Its not as simple as .NET where you can simply stick a [webmethod] tag above your functions. That said, it isnt really that overly complex, just more work.

What you should look for is the NuSoap libary, although the current version from the developers didnt seem to work for me so ill stick it on my server here: http://www.hippacrocapig.com/nusoap.zip

I cant remember the various tutorials I scanned through while learning php webservices but ill post some introductory code here.
<?php

//Include the NuSoap lib
require_once("nusoap/nusoap.php");

//My Vars ignore
require_once("DBAccess/DB.php");
$DB = new DB();


//Create A New Instance Of Your SOAP server and give it a name

$namespace = "Whatever";
$server = new soap_server();
$server->debug_flag = false;
$server->configureWSDL("NameOfYourService", $namespace);
$server->wsdl->schemaTargetNamespace = $namespace;



//Add a complex type (a strut). As I said php is a pain because there is nothing that simply takes your
//Objects and writes the equivelent SOAP to define the structure.

//e.g.  'NameOfProperty'=>array('name' = > 'NameOfProperty', type => 'SOAPType')


$server->wsdl->addComplexType('Message','complexType','struct','all','',
  array(
    'MessageId' => array('name' => 'MessageId','type' => 'xsd:int'),
    'MessageTitle' => array('name' => 'MessageTitle', 'type' => 'xsd:string'),
    'MessageDate' => array('name' => 'MessageDate','type' => 'xsd:date'),
    'MessageDescription' => array('name' => 'MessageDescription', 'type' => 'xsd:string')
         )
);


//Another complex type, this time its an array of the perviosly defined message type.

$server->wsdl->addComplexType(
  'Messages',
  'complexType',
  'array',
  '',
  'SOAP-ENC:Array',
  array(),
  array(
    array('ref' => 'SOAP-ENC:arrayType',
         'wsdl:arrayType' => 'tns:Message[]')
  ),
  'tns:Message'
);


//Now we define the SOAP methods (like when we add [webmethod] above a function in .NET/java)
// You have to state what types will be passed, what will be returned and which function in this script will actually
// be called.

$server->register('GetMessages',                    // method name
  array('Date' => 'xsd:date'),          // input parameters
  array('return' => 'tns:Messages'),    // output parameters
  $namespace,                         // namespace
  $namespace . '#GetMessages',                   // soapaction
  'rpc',                                    // style
  'encoded',                                // use
  'Get Messages For A Set Day'        // documentation
);


//Here where your real codes going to happen, im just passing an array of type "Message" back for example.

function GetMessages($Date)
{
$Messages[0] =

array("MessageId"=> 1,
"MessageTitle"=>"Bah",
"MessageDate"=>"2006-12-12",
"MessageDescription" => "Yippie" );
                        
  return $Messages;
}


//Make sure you have the below headers at the end of your script

$HTTP_RAW_POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA'])
  ? $GLOBALS['HTTP_RAW_POST_DATA'] : '';
$server->service($HTTP_RAW_POST_DATA);
exit();

?>
Now if you stick your service online and add ?wsdl after the url the script returns the expected SOAP message so you can simply reference it with your client program. Your client isnt written in php is it?
My advise however, tell your boss that php is a scripting language great for prototyping and the likes. You can do most things with php that you can with other more complete languages, its just more complex, more time consuming and basically not what the language should be used for. It will end up taking more time, costing more and being a major pain to alter at a later date.
hey thanks a lot for the great reply.

just one more thing.

how about Java?

i have developed web services in .net and they were a piece of cake. all you had to do was to put the web method tag on top and thats it.

is it the same with Java too? because our systems are linux based and we can use .net.


i ll send your reply to my boss. Hope it knocks some sense into him :).

plus i wanted to know that incase my boss turns out be too stubborn and doesnt change his mind what should i use to make web services in PHP.

XMLrpc
SOAP
REST?

which of the three you prefer.

with the survey i did i think using SOAP will be a lot better.

ASKER CERTIFIED SOLUTION
Avatar of icb01co2
icb01co2

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks for the help.
No Probs