PHP implements Soap communication and phpsoap Communication
This example describes how PHP implements Soap communication. Share it with you for your reference. The specific implementation method is as follows:
Copy codeThe Code is as follows: <? Php
Function HttpSoap ($ server, $ port, $ url, $ namespace, $ action, $ data ){
$ Fp = @ fsockopen ($ server, $ port );
If (! $ Fp ){
Return FALSE;
} Else {
$ SoapData = ConstructData ($ namespace, $ action, $ data );
$ Length = strlen ($ soapData );
$ Out = "POST $ url HTTP/1.1 \ r \ n ";
$ Out. = "Host: $ server \ r \ n ";
$ Out. = "Content-Type: text/xml; charset = UTF-8 \ r \ n ";
$ Out. = "Content-Length: $ length \ r \ n ";
$ Out. = "SOAPAction: \" $ namespace $ action \ "\ r \ n ";
$ Out. = $ soapData;
$ Out. = "\ r \ n ";
Fputs ($ fp, $ out );
Stream_set_timeout ($ fp, 2 );
$ Header = "";
While ($ line = trim (fgets ($ fp ))){
$ Header. = $ line. "\ n ";
}
$ DataPos = strpos ($ header, "Content-Length:") + 16;
$ DataEnd = strpos ($ header, "\ n", $ dataPos );
$ DataLength = substr ($ header, $ dataPos, $ dataEnd-$ dataPos );
$ Data = "";
If ($ dataLength> 0 ){
$ Data = fread ($ fp, $ dataLength );
}
Fclose ($ fp );
If (strlen ($ data )! = $ DataLength | $ dataLength <= 0 ){
Return FALSE;
}
Return $ data;
}
}
Function ConstructData ($ namespace, $ action, $ data ){
$ SoapData ="
<? Xml version = "1.0 \" encoding = \ "UTF-8 \"?>
\ R \ n ";
$ SoapData. = "<soap: Envelope xmlns: xsi = \ http://www.w3.org/2001/XMLSchema-instance\ xmlns: xsd = \" http://www.w3.org/2001/XMLSchema\ "xmlns: soap = \ "\ r \ n'> http://schemas.xmlsoap.org/soap/envelope/\"> \ r \ n ";
$ SoapData. = "<soap: Body> \ r \ n ";
$ SoapData. = "<$ action xmlns = \" $ namespace \ "> \ r \ n ";
Foreach ($ data as $ name => $ value ){
$ Name = iconv ("GBK", "UTF-8", $ name );
$ Value = iconv ("GBK", "UTF-8", $ value );
$ SoapData. = "<$ name> $ value </$ name> \ r \ n ";
}
$ SoapData. = "</$ action> \ r \ n ";
$ SoapData. = "</soap: Body> \ r \ n ";
$ SoapData. = "</soap: Envelope> ";
Return $ soapData;
}
<?
$ Data = array (
'User' => 'test', // if you need to input binary data, use BASE64 encoding.
'Pass' => 'test'
);
Echo HttpSoap ('sample .anyhost.com ', 80,'/sampleSoap. asmx ', 'HTTP: // tempuri.org/', 'login', $ data );
?>
I hope this article will help you with PHP programming.
In PHP, you can use the SOAP method to call the API. In this case, the automatically generated API named "ioLiteral" and "ioDocument" is changed.
The end of the file in the interface file generated when you import XML
How does php communicate with delphi through soap?