PHP实现Soap通讯的方法
程序员文章站
2023-11-08 16:11:22
本文实例讲述了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);
?>
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程序设计有所帮助。
上一篇: PHP防止注入攻击实例分析
下一篇: Java多线程并发面试问答