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 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

目录
相关文章
|
3月前
|
前端开发 JavaScript API
阿里云智能媒体服务IMS(Intelligent Media Services)的视频剪辑Web SDK
【1月更文挑战第15天】【1月更文挑战第72篇】阿里云智能媒体服务IMS(Intelligent Media Services)的视频剪辑Web SDK
52 6
|
XML Java 数据格式
大多数人忽略了的Spring官方项目,Spring Web Services
大多数人忽略了的Spring官方项目,Spring Web Services
967 0
|
1月前
|
XML 安全 数据安全/隐私保护
探索 SOAP:揭开 Web 服务的神秘面纱(下)
探索 SOAP:揭开 Web 服务的神秘面纱(下)
|
1月前
|
XML 开发框架 JSON
探索 SOAP:揭开 Web 服务的神秘面纱(上)
探索 SOAP:揭开 Web 服务的神秘面纱(上)
|
7月前
|
弹性计算 安全 数据安全/隐私保护
Internet Information Services(IIS)部署Web项目
本文为您介绍如何快速使用IIS搭建简单网站并发布项目。
226 0
|
12月前
|
XML 缓存 JSON
REST vs SOAP:两种 Web 服务协议的分析
REST(Representational State Transfer)和 SOAP(Simple Object Access Protocol)都是 Web 服务架构的两种主要风格。两者都提供了一种通信方式,可以让不同的应用程序通过网络互相交换数据。但是,它们之间有一些重要的区别。
|
Java Linux API
Java:ews-java-api获取Exchange Web Services (EWS)会议日程
Java:ews-java-api获取Exchange Web Services (EWS)会议日程
477 0
|
XML JavaScript 网络协议
WEB SERVICE名词解释 JSWDL开发包的介绍 JAXP、JAXM的解释 SOAP、UDDI,WSDL解释。
Web ServiceWeb Service是基于网络的、分布式的模块化组件,它执行特定的任务,遵守具体的技术规范,这些规范使得Web Service能与其他兼容的组件进行互操作。
69 0
|
XML 网络协议 JavaScript
Web Services:重整山河待后生
Web Services:重整山河待后生
135 0
Web Services:重整山河待后生
|
监控 Java 区块链
如何在 SAP BTP 平台 ABAP 编程环境里消费基于 SOAP 的 Web Service
如何在 SAP BTP 平台 ABAP 编程环境里消费基于 SOAP 的 Web Service
250 0
如何在 SAP BTP 平台 ABAP 编程环境里消费基于 SOAP 的 Web Service