PHP实现Soap通讯的方法
发布:smiling 来源: PHP粉丝网 添加日期:2021-04-22 11:20:52 浏览: 评论:0
这篇文章主要介绍了PHP实现Soap通讯的方法,是web service的一个典型应用,需要的朋友可以参考下
本文实例讲述了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;
- }//www.phpfensi.com
- <?
- $data=array(
- 'user'=>'测试', //如果需要输入二进制数据请采用BASE64编码
- 'pass'=>'test'
- );
- echo HttpSoap('sample.anyhost.com', 80, '/sampleSoap.asmx', 'http://tempuri.org/', 'logIn', $data);
- ?>
希望本文所述对大家的PHP程序设计有所帮助。
Tags: Soap通讯
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)