Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎專欄 | Search | Email

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;

}

?>