📜 ⬆️ ⬇️

How to make friends java-ws and php4 without using any additions and a little popuit your brain.

I read about working with web services in php using SOAP .
I want to tell a very entertaining story. Once I faced the task of picking up the admin panel of the FastSearch search engine . As they say - do not get used. The only thing that confused me was that the admin panel was written in pkhp 4.0, and I am writing in Java.
Well, pkhp so pkhp. 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 can’t 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, the web service is you, or not a web service, but you work on http and send xml-ku.
I think so - it is unlikely that php4 will not allow me to send xml-ku by http.
In order not to reinvent the wheel and not step on a rake, I pop the header of the soap message that the Toad ws-client sends. This bastard, by the way, eats a link to wsdl (web service description) and offers available service methods, indicating their type.
It turned out this:

/ * sniffed HEADER of SOAP message
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 Javista colleagues, 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 we’ll get the function that will send a dirty soap message to the web service. Praise the balls, I do not need to get an answer from this monster.

/ **
function opens socket, prepares and sends SOAP message. It doesn't read response.
$ data is a SOAP message <Envelope>
* /
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 a 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 .


chic, now we need to form a soap message. In general, there is such a web service envelope (envelope) where I stuff my data. I'll do one more function. Suddenly the web service method signature will change? And in any case, even though I am not on kfp4 and do not code, but I must introduce the elementary element of reuseability.

function write_audit_message ($ source, $ evt, $ server, $ message, $ userIP, $ action) {
$ data = '<soapenv: Envelope xmlns: q0 = " 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 "> '

. '<soapenv: Body>'
. '<q0: addEvent>'
. '<arg_0_0>' . $ source. '</ arg_0_0>'
. '<arg_1_0>' . $ evt. '</ arg_1_0>'
. '<arg_2_0>' . $ server. '</ arg_2_0>'
. '<arg_3_0>' . $ message. '</ arg_3_0>'
. '<arg_4_0>' . $ userIP. '</ arg_5_0>'
. '<arg_5_0>' . $ action. '</ arg_6_0>'
. '</ q0: addEvent>'
. '</ soapenv: Body>'
. '</ soapenv: Envelope>' ;

// 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 the message and remains satisfied.

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

So it's not so simple what is written in two lines!

Source: https://habr.com/ru/post/38461/


All Articles