PHP
PHPを使用してWebサービスを呼び出すには、開発環境を作成して、次を有効にする必要があります。
-
コメント文字のセミコロン(;)を、
extension=php_soap.dll
で始まる行から削除したSoap拡張。 -
セミコロン(;)を、
extension=php_openssl.dll
で始まる行から削除したSSL拡張。
PHP開発環境のインストールと構成の詳細は、EasyPhpを参照してください。
SoapClientを呼び出す単純なフォームを作成するコード例を次に示します。
-
要求を送信するボタンと結果を表示するテキスト領域を組み込んだ単純なHTMLフォーム。
-
要求タイプに基づいて分岐する処理ロジック。 POST要求はWebサービスを呼び出し、他の要求は結果を含むテキスト領域の値をクリアします。
-
Soapクライアントの新規インスタンスを作成する処理ロジック。
-
Webサービスに渡すパラメータを含む配列構造を構築します。
-
応答をXMLとして解析し、出力のテキスト領域に表示される変数に割り当てます。
<?php
// Process POST requests
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
try {
$username ="username";
$password ="password";
// Create new instance for the client
$client = new SoapClient("https://host:port/icCnSetupCreditRulesPublicService/CreditRuleService?wsdl",
array('trace' => 1,
'login' => $username,
'password' => $password));
// Construct the payload to be used to invoke the service.
$params = array(
'findCriteria' => array(
'fetchStart' => '0',
'fetchSize' => '-1',
'filter' => array(
'group' => array(
'upperCaseCompare' => 'false',
'item' => array(
'upperCaseCompare' => 'false',
'attribute' => 'RuleId',
'operator' => '=',
'value' => '300000000851162')
)
),
'excludeAttribute' => 'false'
),
'findControl' => array(
'retrieveAllTranslations' => 'false')
);
// Invoke the service
$response = $client->FindRule($params);
// Process the response. In this example, the content is copied to a variable that is displayed as output
$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($client->__getLastResponse());
$dom->formatOutput = TRUE;
$dom->saveXml();
$output = $dom->saveXml();
} catch(Exception $e) {
print "Exception: " . $e->getMessage();
}
} else {
// For GET request, set the variable displayed as output
$output = "";
}
?>
<form name="envelope" method="POST">
<!-- Button used to start the processing -->
<input id="invoke_service" type="submit" value="Invoke Service" />
<!-- Text area to display the result for processing. As the result is XML, it is encoded; otherwise it is not displayed correctly -->
<textarea name="outputpayload" wrap="off" style="width:100%;height:100%" rows=30><?php echo
htmlentities($output);?></textarea>
</form>