T086学习网 | 站长学院 | 技术文档 | 成语 | 歇后语 | 帝国时代 | 代码收藏 | IP地址查询 | 生活百科 | 生日密码 | CSS压缩 | 用户评论 | 欣欣百宝箱

用php实现soap通讯

【 网络作者:佚名 更新时间:2009-11-23 | 字体:
[导读]用PHP实现Soap通讯 近日用PHP实现Soap通讯,写了一些简单的方法:引用文字<?phpfunction HttpSoap($server, $port, $url, $namespace, $action, $data) {$fp = @fsockopen($server, $port);if (!$fp) {return FAL...

用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 ";
$out .= "Host: $server ";
$out .= "Content-Type: text/xml; charset=utf-8 ";
$out .= "Content-Length: $length ";
$out .= "SOAPAction: ""$namespace$action"" ";
$out .= $soapData;
$out .= " ";

fputs($fp, $out);
stream_set_timeout($fp, 2);

$header = "";
while($line = trim(fgets($fp))) {
$header .= $line." ";
}
$dataPos = strpos($header, "Content-Length: ") + 16;
$dataEnd = strpos($header, " ", $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""?>

";
$soapData .= "<soap:Envelope xmlns:xsi=http://www.w3.org/2001/XMLSchema-instancexmlns:xsd=""http://www.w3.org/2001/XMLSchema" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""> ";
$soapData .= " <soap:Body> ";
$soapData .= " <$action xmlns=""$namespace""> ";
foreach($data as $name => $value) {
$name = iconv("GBK","UTF-8",$name);
$value= iconv("GBK","UTF-8",$value);
$soapData .= " <$name>$value</$name> ";
}
$soapData .= " </$action> ";
$soapData .= " </soap:Body> ";
$soapData .= "</soap:Envelope>";

return $soapData;
}<?
$data=array(
'user'=>'测试', //如果需要输入二进制数据请采用BASE64编码
'pass'=>'test'
);
echo HttpSoap('sample.anyhost.com', 80, '/sampleSoap.asmx', 'http://tempuri.org/', 'logIn', $data);
?>

注:php5以上自带有soap了

  • 转载请注明来源:IT学习网 网址:http://www.t086.com/ 向您的朋友推荐此文章
  • 特别声明: 本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系我们,我们会尽快予以更正。
更多
留言建议ASP探针PHP探针站长Enjoy的Blog
© 2017 T086学习网 - T086.com(原itlearner.com)
RunTime:7.33ms QueryTime:7