PHPでSOAPクライアント

CentOS5.5でやってみました。

phpのインストール

# yum install --enablerepo=webtatic php php-soap
# rpm -qa | grep php
php-common-5.3.10-1.w5
php-5.3.10-1.w5
php-cli-5.3.10-1.w5
php-soap-5.3.10-1.w5

ネタ

ネタは、Google Apps Scriptのドキュメント内で、サンプルとされていたGEOサービスを利用します。
しかしながらGAS、良くできております。

Tutorial: Accessing a Soap Service - Google Apps Script ? Google Developers

function determineCountryFromIP(ipAddress) {
    var wsdl = SoapService.wsdl("http://www.webservicex.net/geoipservice.asmx?wsdl");
    var geoService = wsdl.getGeoIPService();

    var param = Xml.element("GetGeoIP", [
                  Xml.attribute("xmlns", "http://www.webservicex.net/"),
                  Xml.element("IPAddress", [
                    ipAddress
                  ])
                ]);

    var result = geoService.GetGeoIP(param);
    return result.Envelope.Body.GetGeoIPResponse.GetGeoIPResult.CountryCode.Text;
}

PHPで同じものを書いてみる

こんな感じになりました。

<?php
/*-- geo.php --*/
function determineCountryFromIP($ipAddress) {
        try {
                $client = new SoapClient("http://www.webservicex.net/geoipservice.asmx?wsdl",
                                array('cache_wsdl' => WSDL_CACHE_NONE, 'trace' => 1, 'exceptions' => true 
                                     )
                                );
/*
                foreach ($client->__getFunctions() as $func) {
                        print "$func \n";
                }
*/
                $params = array( "IPAddress" => "$ipAddress" );

                $res = $client->GetGeoIP($params);
                var_dump($res);
        } catch (SoapFault $soapFault) {
                echo $soapFault->getCode().":".$soapFault->getMessage();
        } catch (Exception $e) {
                echo 'Caught exception: ',  $e->getMessage(), "\n";
        }
}

determineCountryFromIP('124.83.187.140');
実行結果
# php -f geo.php 
object(stdClass)#2 (1) {
  ["GetGeoIPResult"]=>
  object(stdClass)#3 (5) {
    ["ReturnCode"]=>
    int(1)
    ["IP"]=>
    string(14) "124.83.187.140"
    ["ReturnCodeDetails"]=>
    string(7) "Success"
    ["CountryName"]=>
    string(5) "Japan"
    ["CountryCode"]=>
    string(3) "JPN"
  }
}

SOAPのメッセージをトレースするには

プロキシを介するようにして、そこで覗いた方が良さそうです。

私の場合はPCでBurp Suite(プロキシするソフト)を起動します。


SoapClientのコンストラクタに、プロキシ情報(例では192.168.100.111:8080)を渡します。

<?php
$client = new SoapClient("http://www.webservicex.net/geoipservice.asmx?wsdl",
    array('cache_wsdl' => WSDL_CACHE_NONE, 'trace' => 1, 'exceptions' => true, 
        'proxy_host' => '192.168.100.111',
        'proxy_port' => 8080
    )
);

こうして実行すると、Burp Suite側でリクエストとレスポンスのメッセージを見ることができます。