Building web services in PHP

Posted on 2006-06-06
Last Modified: 2008-02-20

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++.


Question by:zorawar_bahadur
    LVL 6

    Expert Comment

    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.

    Author Comment


    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.

    ... ...

    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


    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.

    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

    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.

    LVL 6

    Expert Comment

    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.

    Author Comment

    LVL 1

    Expert Comment

    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:

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

    Expert Comment


    //Include the NuSoap lib

    //My Vars ignore
    $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')

        '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.

        array('ref' => 'SOAP-ENC:arrayType',
             'wsdl:arrayType' => '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,
    "MessageDescription" => "Yippie" );
      return $Messages;

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

      ? $GLOBALS['HTTP_RAW_POST_DATA'] : '';

    LVL 1

    Expert Comment

    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?
    LVL 1

    Expert Comment

    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.

    Author Comment

    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.


    which of the three you prefer.

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

    LVL 1

    Accepted Solution

    Hi zorawar_bahadur,

    Just wrote a bit SOAP vs REST argument and then my computer crashed. Ill summarize:

    - REST you define what is passed back with SOAP+wsdl you have a standard message returned.
    - REST passes data via the url and has no need for external packages like NuSoap etc to generate the wsdl
    - wsdl is standard message specification and hence good tool support, espesially with Visual Studios as microsoft is really pushing the technology.

    Basically if your passing complex objects to and from the service then it's probably better to use SOAP and the assosiated packages. Otherwise if you can handle writing and retrieving your own message type for the responses then go for the REST route.

    Here are examples of how to use services with REST, SOAP and XML-rpc

    As for Java webservice support i dont know to be honest, but i'd imagine you'd have to searcg google for appropriate class libaries. I think maybe Suns net beans IDE has good built in suppot though, but agin not sure.


    Author Comment

    thanks for the help.
    LVL 1

    Expert Comment

    No Probs

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to mak…
    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now