用php实现soap通讯,用php实现soap通讯
【 tulaoshi.com - PHP 】
用PHP实现Soap通讯 近日用PHP实现Soap通讯,写了一些简单的方法:引用文字<?
phpfunction 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.1rn";$out .= "Host: $serverrn";$out .= "Content-Type: text/xml; charset=utf-8rn";$out .= "Content-Length: $lengthrn";$out .= "SOAPAction: "$namespace$action"rnrn";$out .= $soapData;$out .= "rnrn";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"?
rn";$soapData .= "<soap:Envelope xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xml
soap.org/soap/envelope/"rn";$soapData .= " <soap:Bodyrn";$soapData .= " <$action xmlns="$namespace"rn";foreach($data as $name = $value) {$name = iconv("GBK","UTF-8",$name);$value= iconv("GBK","UTF-8",$value);$soapData .= " <$name$value</$namern";}$soapData .= " </$actionrn";$soapData .= " </soap:Bodyrn";$soapData .= "</soap:Envelope";return $soapData;}
<?
$data=array(
'user'='测试', //如果需要输入二进制数据请采用BASE64编码
'pass'='test'
);
echo HttpSoap('
来源:http://www.tulaoshi.com/n/20160129/1491083.html
看过《用php实现soap通讯》的人还看了以下文章 更多>>