トップ 差分 一覧 ソース 置換 検索 ヘルプ PDF RSS ログイン

PHPでSOAP

概要

PHP5以降はPHPにSOAPを使う機能がある。
それ以前はPEAR::SOAPを使う。

PHP SOAP

 サーバ

注意:handle前にhtmlなどのレスポンスをすると動作しない。

 <?php
   class MySoapServer
   {
     public function getMessage()
     {
       return 'Hello,World!';
     }
     
     public function addNumbers($num1,$num2)
     {
       return $num1+$num2;
     }
   }
   $options= array('uri'=>'http://localhost/test');
   $server=new SoapServer(NULL,$options);
   $server->setClass('MySoapServer');
   $server->handle();
 ?>

 クライアント

 <?php
   $options= array(
     'location' 	=>	'http://xxx.xxx.xxx.xxx/server.php',
     'uri'		=>	'http://localhost/test'
     );
   $client=new SoapClient(NULL,$options);
   echo $client->getMessage();  //Hello,World!
   echo $client->addNumbers(3,5); //  8 
 ?>

PEAR::SOAP

http://codezine.jp/article/detail/199
http://isopan.blog.fc2.com/blog-entry-21.html

 サーバ

 <?php
   require_once('SOAP/Server.php');
   
   function getMethod($str, $str2){
     $rs = $str . '123' . $str2;
     return $rs;
   }
   
   $server = new SOAP_Server();
   $server->addToMap('getMethod', array(), array());
   $server->service($HTTP_RAW_POST_DATA);
   
 ?>

 クライアント

 <?php
   require_once('SOAP/Client.php');
   
   $client = new SOAP_Client('http://xxx.xxx.xxx.xxx/~hoge/server.php');
   $params = array('hello', 'world');
   $options = array('timeout' => 20);
   //リクエストするメソッド名を記述
   $result = $client->call('getMethod', $params, $options);
   //出力結果 hello123world
   echo $result; 
 ?>

[カテゴリ: プログラミング言語 > PHP]

[通知用URL]



  • Hatenaブックマークに追加
  • livedoorクリップに追加
  • del.icio.usに追加
  • FC2ブックマークに追加

最終更新時間:2015年11月29日 14時23分31秒