Chinaunix首页 | 论坛 | 博客
  • 博客访问: 18931506
  • 博文数量: 7460
  • 博客积分: 10434
  • 博客等级: 上将
  • 技术积分: 78178
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-02 22:54
文章分类

全部博文(7460)

文章存档

2011年(1)

2009年(669)

2008年(6790)

分类:

2008-04-12 19:41:46

用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 .= " xmlns:xsd=\"\" xmlns:soap=\"
soap.org/soap/envelope/\">\r\n"; $soapData .= " \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\r\n"; } $soapData .= " \r\n"; $soapData .= " \r\n"; $soapData .= "
"; return $soapData; }
  1. $data=array(
  2. 'user'=>'测试', //如果需要输入二进制数据请采用BASE64编码
  3. 'pass'=>'test'
  4. ); 
  5. echo HttpSoap('sample.anyhost.com', 80, '/sampleSoap.asmx', '', 'logIn', $data);
  6. ?>
 
阅读(244) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~