How to make friends java-ws and php4 without using any add-ons and a little troubles with your brain.

    I read about Working with web services in php through SOAP .
    I want to tell a very interesting story. Once, I had the task of picking up the admin panel of the FastSearch search engine . As they say - do not get used to it. The only thing that bothered me was that the admin panel was written in php 4.0, and I write in Java.
    Well, php so php. Naturally, Google immediately gave me Pear: Soap. How would you feed him wsld from a web service and everyone will start laughing and crying with happiness. Either because I don’t know how to read examples, or because the web service was written in Java and hung out on IBM WebSphere Application Server 6.0, and IBM, as you know, is ahead of the rest, loves to do standardized things in its own way , in general Pear: Soap did not work.

    Well, you are a web service, or not a web service, but work on http and send xml-ku.
    I think so - it is unlikely that php4 will not let me send xml-ku via http.
    In order not to reinvent the wheel and not step on the rake, I sniffed the header of the soap message that the Toad ws-client sends. This bastard, by the way, eats a link to wsdl (description of a web service) and offers available service methods, indicating their type.
    This is what happened :

    / * sniffed HEADER of SOAP message use it as template
    POST / AuditServiceEAR / services / AuditService HTTP / 1.1

    Host: superhost.ru:9081
    Content-Type: text / xml; charset = utf-8
    Content-Length: 589
    Accept: application / soap + xml, application / dime, multipart / related, text / *
    User-Agent: IBM Web Services Explorer

    Cache-Control: no-cache
    Pragma: no-cache
    SOAPAction: ""
    Connection: close
    * /

    This is already something.
    Now it's time to talk a little code.
    To make it easier for fellow Javists, I write simply and without taste:

    / * CONSTANTS * /
    $ URL_TO_WEB_SERVICE = " superhost.ru/AuditServiceEAR/services/AuditService "; // path to web-service
    $ PORT_NUMBER = 9081; * This source code was highlighted with Source Code Highlighter .


    Now make a function that will send an obscene soap-message to a web service. Praise the eggs, I do not need to receive an answer from this monster.

    / **
      function opens socket, prepares and sends SOAP message. It doesn't read response.
      $ data is a SOAP message
    * /
    function do_post_request ($ data) {
       global $ PORT_NUMBER, $ URL_TO_WEB_SERVICE;

       
       $ url = $ URL_WEB_SERVICE;
       $ start = strpos ($ url, '//') + 2;
       $ end = strpos ($ url, '/', $ start);
       $ host = substr ($ url, $ start, $ end- $ start);
       $ domain = substr ($ url, $ end);
       
       $ fp = pfsockopen ($ host, $ PORT_NUMBER); // it is persistent version of fsockopen (). It behaves the same way. But it doesn't close connection

       if (! $ Fp) {
           return null;
       }
        fputs ($ fp, "POST $ domain HTTP / 1.1 \ n");            
        fputs ($ fp, "Host: $ host \ n");               
        fputs ($ fp, "Content-type: text / xml; charset = utf-8 \ n");

        fputs ($ fp, "Content-length:„ .strlen ($ data). “\ n");      
        fputs ($ fp, "Accept: application / soap + xml, application / dime, multipart / related, text / * \ n");
        fputs ($ fp, "Cache-Control: no-cache \ n");

        fputs ($ fp, "Pragma: no-cache \ n"); 
        fputs ($ fp, 'SOAPAction: "" \ n');
        fputs ($ fp, “Connection: close \ n \ n”);
        fputs ($ fp, "$ data \ n \ n");
        fclose ($ fp);
    } * This source code was highlighted with Source Code Highlighter .


    gorgeous, now we need to form a soap message. In general, there is such a web service envelope (envelope), where I will cram my data. I'll do one more function. Suddenly the signature of the web service method will change? And in any case, even though I do not code on php4, I have to introduce an elementary element of re-usability.

    function write_audit_message ($ source, $ evt, $ server, $ message, $ userIP, $ action) {
       $ data = 'audit.service.company "xmlns: soapenv =" schemas.xmlsoap.org/soap/envelope "xmlns: xsd =" www.w3.org/2001/XMLSchema "xmlns: xsi =" www.w3.org/2001/XMLSchema -instance "> '

           .''
          .''
           .''. $ source.''
           .''. $ evt.''
           .''. $ server.''
           .''. $ message.''
           .''. $ userIP.' '
            .''. $ action.' '
           .'
    '
           .'
    '
           .'
    ';

        // now send prepared envelope
       do_post_request ($ data);
    } * This source code was highlighted with Source Code Highlighter .


    In general, everything is ready. The web service is located, receives a message and is satisfied.

    Sometimes, possessing the most modern means, you can be in the ass. Yes, you can send a soap message in three lines, yes php4 is the day before yesterday, but what now to do if you have at hand the most primitive means and a smart header and body of a soap message.

    So it’s not so simple that it is written in two lines!


    Also popular now: