2

Guzzleを使用して Web サービスにリクエストを送信します。

次のような JSON ファイルがあります。

{
    "name": "Webservice name",
    "apiVersion": "1.0",
    "description": "description",
    "operations": {
        "commandName1": {
            "httpMethod": "POST",
            "uri": "some/uri/to/some/resource/{value}",
            "summary": "description",
            "parameters": {
                "value": {
                    "location": "uri",
                    "description": "description"
                }
            }
        },
        "commandName2": {
            "httpMethod": "POST",
            "uri": "some/uri/to/some/resource/{value}",
            "summary": "description",
            "parameters": {
                "value": {
                    "location": "uri",
                    "description": "description"
                }
            }
        }
    }
}

それを使用するコードは次のようになります。

$client = new Client(); // instance of Guzzle\Service\Client

$this->client->setDefaultOption(
    'auth',
    array('admin', 'admin', 'Basic')
);

$this->client->setDefaultOption(
    'headers',
    array('Accept' => 'text/html', 'Content-Type' => 'text/html')
);

$description = ServiceDescription::factory('/path/to/json/file/with/routes');
$client->setDescription($description);

$params = array(
    'command.request_options' = array(
        'timeout'         => 5,
        'connect_timeout' => 2
    )
);

$command = $client->getCommand('commandName1', $params);
$command->prepare();

$client->execute($command);

ご覧のとおり、PHP コードでContent-TypeAcceptヘッダーを指定しています。その情報を JSON ファイルに移動し、操作ごとに異なる値を指定する方法はありますか? 例: 「commandName1」にはコンテンツ タイプとして HTML が必要ですが、「commandName2」には JSON が必要です。

多くのコードの重複を避けるためにこれを行いたいです。

過去 2 時間、Web と Guzzle のドキュメントを調べたところ、何も表示されませんでした。しかし、私の意見では、ドキュメンテーションの書き方がまずく1あり、過去に読んでいる間に見逃していました。だから再発の可能性大。

誰かがこのようなことをしなければならなかったことがありますか? どのように解決しましたか?前もって感謝します。

1 = 「書き方が悪い」とは、実際にはすべての部分が不完全であることを意味します。すべての章が主題に触れているように見えますが、パラメータ、メソッドなど、またはその完全な機能について、実際に完全または詳細な説明を提供することは決してありません。SSCCEであるコード スニペットがないため、2 分以内にコピーして貼り付けるだけで目の前で機能することがわかります。しかし、これは別の話題です...

4

1 に答える 1