3.13. Web Services / SOAP

简介:

3.13.1. Server

		
'encoding'=>'UTF-8'
		
		

3.13.1.1. addFunction

			
function echoString($inputString)
{
    return $inputString;
}

function echoTwoStrings($inputString1, $inputString2)
{
    return array("outputString1" => $inputString1,
                 "outputString2" => $inputString2);
}

$server = new SoapServer(null, array('uri' => "http://192.168.2.15"));
$server->addFunction("echoString");
$server->addFunction("echoTwoStrings");

$server->addFunction(SOAP_FUNCTIONS_ALL);
$server->handle();
			
			
			
<?php

$options = array('uri' => "http://192.168.2.15",
                'location'=>'http://192.168.2.15/soapserver.php',
                'trace'=>true);
$client = new SoapClient(null, $options);
echo $client->echoString("aaa");
print_r($client->echoTwoStrings('B','A'));
			
			

3.13.1.2. setClass

			
<?php
Class Test{
    public function hello($val){
        return ($val);
    }
	public function sum($v1,$v2){
		return($v1+$v2);
	}
}

$server = new SoapServer(null, array('uri' => "http://192.168.2.15"));
$server->setClass("Test");
$server->handle();
			
			
			
<?php
$options = array('uri' => "http://192.168.2.15",
                'location'=>'http://192.168.2.15/soapserver.php',
                'trace'=>true);
$client = new SoapClient(null, $options);
echo $client->hello("Hello");
print_r($client->sum(10,20));
			
			

3.13.2. SoapClient

$options

ssl_method

'ssl_method' => SOAP_SSL_METHOD_SSLv3
		

3.13.3. HTTP 验证

配置 Nginx

		
server {
    listen       80;
    server_name  api.example.com;

    charset utf-8;
    access_log  /var/log/nginx/api.example.com.access.log  main;
    auth_basic            "Login";
    auth_basic_user_file  htpasswd;

    location / {
        root   /www/example.com/api.example.com;
        index  index.html index.php;

    }
    ...
    ...
}    		
		
		

创建密码文件,请参考《Netkiller Web 手札》

# cat /etc/nginx/htpasswd 
neo:$apr1$mnT/iqg5$gn7m7xx.eflX9VK6p8hyj0		
		

SoapClient 需要 login与password两个选项

		
<?php

$options = array('uri' => "http://api.example.com",
                'location'=>'http://api.example.com/soapserver.php',
				'login'=>'neo',
				'password'=>'chen',
                'trace'=>true
				);
$client = new SoapClient(null, $options);

try { 
	echo $client->hello("Hello");
	print_r($client->sum(10,20));
   
} 
catch (Exception $e) 
{ 
    echo 'Caught exception: ',  $e->getMessage(), "\n"; 
} 
		
		

3.13.4. Example

3.13.4.1. addFunction 实例

soapfunc.php

			
$ cat soapfunc.php
<?php

function reverse($str){

        $retval = '';

        if(strlen($str) < 1) {

                return new SoapFault('Client','','Invalid string');

        }

        for ($i = 1; $i <= strlen($str); $i++) {

                $retval .= $str[(strlen($str) - $i)];

        }

        return $retval;

}

function sum($num1, $num2) {

        if (trim($num1) != intval($num1)) {

                return new SoapFault('Client','','The first number is invalid');

        }

        if (trim($num2) != intval($num2)) {

                return new SoapFault('Client','','The second number is invalid');

        }

        return ($num1 + $num2);

}

function gettime(){

        $time=strftime("%Y-%m-%d %H:%M:%S");

        return $time;

}

?>
			
			

soapserver.php

			
$ cat soapserver.php
<?php
include_once('soapfunc.php');

$soap = new SoapServer(null,array('uri'=>"http://netkiller.6600.org/"));

$soap->addFunction('reverse');

$soap->addFunction('sum');

$soap->addFunction('gettime');

$soap->addFunction(SOAP_FUNCTIONS_ALL);

$soap->handle();

?>
			
			

soapclient.php

			
$ cat soapclient.php
<?php

try {

        $client = new SoapClient(null, array('location' =>"http://netkiller.6600.org/soapserver.php",'uri' => "http://netkiller.6600.org/"));

        $str = "This string will be reversed";

        $reversed = $client->reverse($str);

        echo "If you reverse '",$str,"', you get '",$reversed,"' </br>";

        $n1=50;

        $n2=130;

        $sum = $client->sum($n1,$n2);

        echo "If you try ",$n1,"+",$n2,", you will get ",$sum,"</br>";

        echo "The system time is: ",$client->gettime();

} catch (SoapFault $fault){

        echo "Fault! code:",$fault->faultcode,", string: ",$fault->faultstring;

}

?>
			
			




原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

目录
相关文章
|
4月前
|
前端开发 JavaScript API
阿里云智能媒体服务IMS(Intelligent Media Services)的视频剪辑Web SDK
【1月更文挑战第15天】【1月更文挑战第72篇】阿里云智能媒体服务IMS(Intelligent Media Services)的视频剪辑Web SDK
116 6
|
XML Java 数据格式
大多数人忽略了的Spring官方项目,Spring Web Services
大多数人忽略了的Spring官方项目,Spring Web Services
1135 0
|
3月前
|
XML 前端开发 JavaScript
RESTful Web Services
RESTful Web Services
21 2
|
4月前
|
XML 网络协议 Java
XML Web 服务技术解析:WSDL 与 SOAP 原理、应用案例一览
XML Web服务是基于WSDL、SOAP、RDF和RSS等标准的网络应用程序组件技术。WSDL描述服务接口和消息格式,SOAP用于结构化信息交换,RDF描述网络资源,RSS则用于发布网站更新。Web服务特点是自包含、自描述,基于开放协议,可重用且能连接现有软件。WSDL文档包含`types`、`message`、`portType`和`binding`元素,定义服务操作和协议。SOAP协议规定消息格式,通过HTTP等传输。
525 1
|
4月前
|
Ubuntu Linux 网络安全
在Amazon Web Services中使用R语言运行模拟
在Amazon Web Services中使用R语言运行模拟
|
4月前
|
XML 开发框架 JSON
探索 SOAP:揭开 Web 服务的神秘面纱(上)
探索 SOAP:揭开 Web 服务的神秘面纱(上)
|
4月前
|
XML 安全 数据安全/隐私保护
探索 SOAP:揭开 Web 服务的神秘面纱(下)
探索 SOAP:揭开 Web 服务的神秘面纱(下)
|
12月前
|
弹性计算 安全 数据安全/隐私保护
Internet Information Services(IIS)部署Web项目
本文为您介绍如何快速使用IIS搭建简单网站并发布项目。
321 0
|
Java Linux API
Java:ews-java-api获取Exchange Web Services (EWS)会议日程
Java:ews-java-api获取Exchange Web Services (EWS)会议日程
621 0
|
XML 缓存 JSON
REST vs SOAP:两种 Web 服务协议的分析
REST(Representational State Transfer)和 SOAP(Simple Object Access Protocol)都是 Web 服务架构的两种主要风格。两者都提供了一种通信方式,可以让不同的应用程序通过网络互相交换数据。但是,它们之间有一些重要的区别。
下一篇
云函数