本文小编为大家详细介绍“如何用PHP实现Soap通讯”,内容详细,步骤清晰,细节处理妥当,希望这篇“如何用PHP实现Soap通讯”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
代码如下:
<? 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\r\n"; $out .= $soapData; $out .= "\r\n\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'=>'测试', //如果需要输入二进制数据请采用BASE64编码 'pass'=>'test' ); echo HttpSoap('sample.anyhost.com', 80, '/sampleSoap.asmx', 'http://tempuri.org/', 'logIn', $data); ?>
读到这里,这篇“如何用PHP实现Soap通讯”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。