PHP implements Soap communication and phpsoap Communication

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.