首页 > 资讯列表 > 编程/数据库 >> PHP

PHP实现Soap通讯的示例代码

PHP 2014-12-02 00:03:56 转载来源: 网络整理/侵权必删

本文是一个PHP实现Soap通讯的示例,是web service的一个典型应用,需要的朋友可以参考下 代码如下: <?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.1rn"; $out

本文是一个PHP实现Soap通讯示例,是web service的一个典型应用,需要的朋友可以参考下

代码如下:
<?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.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="rn'>http://schemas.xmlsoap.org/soap/envelope/">rn";
$soapData .= " <soap:Body>rn";
$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</$name>rn";
}
$soapData .= " </$action>rn";
$soapData .= " </soap:Body>rn";
$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 通讯 示例 代码


声明:本文内容来源自网络,文字、图片等素材版权属于原作者,平台转载素材出于传递更多信息,文章内容仅供参考与学习,切勿作为商业目的使用。如果侵害了您的合法权益,请您及时与我们联系,我们会在第一时间进行处理!我们尊重版权,也致力于保护版权,站搜网感谢您的分享!

站长搜索

http://www.adminso.com

Copyright @ 2007~2024 All Rights Reserved.

Powered By 站长搜索

打开手机扫描上面的二维码打开手机版


使用手机软件扫描微信二维码

关注我们可获取更多热点资讯

站长搜索目录系统技术支持