Home >Backend Development >PHP Tutorial >How to implement Soap communication in PHP, phpsoap communication_PHP tutorial

How to implement Soap communication in PHP, phpsoap communication_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:15:281126browse

How PHP implements Soap communication, phpsoap communication

The example in this article describes the method of implementing Soap communication in PHP. Share it with everyone for your reference. The specific implementation method is as follows:

Copy code The code is as follows:
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.1rn";
$out .= "Host: $serverrn";
$out .= "Content-Type: text/xml; charset=utf-8rn";
$out .= "Content-Length: $lengthrn";
$out .= "SOAPAction: "$namespace$action"rnrn";
$out .= $soapData;
$out .= "rnrn";
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 = "

rn";
$soapData .= "rn";
$soapData .= " rn";
$soapData .= " <$action xmlns="$namespace">rn";
foreach($data as $name => $value) {
$name = iconv("GBK","UTF-8",$name);
$value= iconv("GBK","UTF-8",$value);
$soapData .= " <$name>$valuern";
}
$soapData .= " rn";
$soapData .= "
rn";
$soapData .= "
";
return $soapData;
}
$data=array(
'user'=>'Test', //If you need to enter binary data, please use BASE64 encoding
'pass'=>'test'
);
echo HttpSoap('sample.anyhost.com', 80, '/sampleSoap.asmx', 'http://tempuri.org/', 'logIn', $data);
?>

I hope this article will be helpful to everyone’s PHP programming design.

Ask to use SOAP method to call in PHP, where change the automatically generated by delphi: ioLiteral and ioDocument

The end of the file in the interface file generated when you import XML

How to communicate with Delphi through soap

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/904921.htmlTechArticleHow PHP implements Soap communication, phpsoap communication This article describes how PHP implements Soap communication. Share it with everyone for your reference. The specific implementation method is as follows: Copy the code The code is as follows...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn