クライアントからサーバーの関数を実行することができます。
注意点としてSOAPはUTF-8で通信を行います。
◆クライアント(soap_server.php)
$location = 'http://localhost/test/soap_server.php'; // サーバーURL
$uri = 'http://localhost/test/';
try {
$client = new SoapClient(null,array(
'location'=> $location,
'uri' => $uri,
'trace' => 1
));
$result = $client->hello(mb_convert_encoding("なまえ", 'utf-8'));
echo mb_convert_encoding($result, mb_internal_encoding(), 'utf-8');
$user = new stdClass();
$user->name = mb_convert_encoding("はろーわーるど", 'utf-8');
$user->url = 'http://localhost/';
$result = $client->helloByObj($user);
echo mb_convert_encoding($result, mb_internal_encoding(), 'utf-8');
}
catch (Exception $e) {
}
◆サーバー(soap_server.php)
mb_internal_encoding('utf-8');
mb_http_output('utf-8');
//関数
function hello($nm){
return 'こんにちは、' . $nm . ' さん!';
}
function helloByObj($obj){
return 'こんにちは、' . $obj->name . ' さん!';
}
// SOAPサーバオブジェクトの作成
$server = new SoapServer(null, array('uri' => 'http://localhost/test/'));
// サービスの追加
$server->addFunction('hello');
$server->addFunction('helloByObj');
// サービスを実行
$server->handle();PHP ブログランキングへ