1. 云栖社区>
  2. PHP教程>
  3. 正文

用php实现soap通讯

作者:用户 来源:互联网 时间:2017-12-01 10:34:07

php通讯

用php实现soap通讯 - 摘要: 本文讲的是用php实现soap通讯, 用PHP实现Soap通讯 近日用PHP实现Soap通讯,写了一些简单的方法:引用文字 <? phpfunction HttpSoap($server, $port, $url, $namespace, $action, $data

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通讯的全部内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有其他相关内容,欢迎继续使用右上角搜索按钮进行搜索php 通讯 ,以便于您获取更多的相关知识。

弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率

40+云计算产品,6个月免费体验

现在注册,免费体验40+云产品,及域名优惠!

云服务器9.9元/月,大学必备
Apache Spark Meetup China 第1期 最全资料下载