Windows版PHP 4.3.10にインストールしてみた。
C:\php\>pear install SOAP-beta downloading SOAP-0.8.1.tgz ... Starting to download SOAP-0.8.1.tgz (69,177 bytes) .................done: 69,177 bytes requires package `Mail_Mime' requires package `HTTP_Request' requires package `Net_URL' requires package `Net_DIME' SOAP: Dependencies failed
依存関係でエラー。必要パッケージをチェック
pear info パーケージ名
バージョンが古いと言われたら
pear upgrade パーケージ名
必要パッケージのインストール
pear install Mail_Mime pear install Net_URL pear install HTTP_Request pear install Net_DIME-beta pear install SOAP-beta
Fedora Core 1の場合は、上記以外にアップグレードが必要だった。
pear upgrade Net_Socket
これで無事インストール完了。
「sayHello('名前')」という関数を作成してみる。
名前を引数に渡すと「Hello 名前」と呼んでくれる簡単なもの。
wsdlも勝手に作成してくれるので便利。
| wsdlの位置を知らせてくれる | hello_server.php |
|---|---|
| wsdl | hello_server.php?wsdl |
hello_server.php
<?php
require_once 'SOAP/Server.php';
// Your class
class HelloServer {
var $__dispatch_map = array();
function HelloServer() {
// Define the signature of the dispatch map
$this->__dispatch_map['sayHello'] =
array('in' => array('inputString' => 'string'),
'out' => array('outputString' => 'string'),
);
}
// Required function by SOAP_Server
function __dispatch($methodname) {
if (isset($this->__dispatch_map[$methodname]))
return $this->__dispatch_map[$methodname];
return NULL;
}
// Your function
function sayHello($inputString)
{
return 'Hello '.$inputString;
}
}
// Fire up PEAR::SOAP_Server
$server = new SOAP_Server;
// Fire up your class
$helloServer = new HelloServer();
// Add your object to SOAP server (note namespace)
$server->addObjectMap($helloServer,'urn:HelloServer');
// Handle SOAP requests coming is as POST data
if (isset($_SERVER['REQUEST_METHOD']) &&
$_SERVER['REQUEST_METHOD']=='POST') {
$server->service($HTTP_RAW_POST_DATA);
} else {
// Deal with WSDL / Disco here
require_once 'SOAP/Disco.php';
// Create the Disco server
$disco = new SOAP_DISCO_Server($server,'HelloServer');
header("Content-type: text/xml");
if (isset($_SERVER['QUERY_STRING']) &&
strcasecmp($_SERVER['QUERY_STRING'],'wsdl')==0) {
echo $disco->getWSDL(); // if we're talking http://www.example.com/index.php?wsdl
} else {
echo $disco->getDISCO();
}
exit;
}
?>
SOAPサーバーにある、「sayHello()」関数を呼んでみる。
<?php
require_once 'SOAP/Client.php';
// Modify the URL here - note the "?wsdl" at the end
$url = 'http://' . $_SERVER["HTTP_HOST"] . dirname($_SERVER["REQUEST_URI"]) . '/hello_server.php?wsdl';
$wsdl = new SOAP_WSDL ($url);
$helloClient = $wsdl->getProxy();
echo ( $helloClient->sayHello('Bill') );
?>
検索には数秒要するが結果を表示できた。
<?
require_once('SOAP/Client.php');
?>
<form action="<? echo $PHP_SELF?>" method="get">
<input type="radio" name="type" value="zipcode" checked>郵便番号
<input type="radio" name="type" value="address">住所
<input type="text" name="query" value="<?php echo isset($_GET["query"]) ? $_GET["query"] : ""; ?>">
<input type="submit">
</form>
<?php
if (!isset($_GET["type"])) exit(1);
$query = mb_convert_encoding(trim($_GET["query"]), "utf-8", "auto");
$type = trim($_GET["type"]);
$g_WSDL = "http://nile.esm.co.jp:8080/wsdl/ZIPSearch.wsdl";
/**
* プロキシの作成
*/
$wsdl = new SOAP_WSDL($g_WSDL);
$proxy = $wsdl->getProxy();
$result = array();
if($type == "zipcode"){
$result = $proxy->getAddressByZipcode($query);
}else{
$result = $proxy->getZipcodeByAddress($query);
}
if(!is_array($result)){ // 配列じゃなきゃ失敗
echo "該当なし。<br>\n";
return;
}
foreach($result as $key=>$val){
// echo mb_convert_encoding($key, mb_internal_encoding(), 'auto') . " => ";
echo mb_convert_encoding($val, mb_internal_encoding(), 'auto') . "<br>\n";
}
?>var_export( (array)$response );