httprequest - 我如何在 pecl_http 库中使用发布请求

标签 httprequest pecl

hotelbeds api

需要用一些字段的post请求的api,但我不知道字段将被添加到哪里!! (在 GET 请求中,我像任何请求一样在 url 中添加字段)

API 代码

`

$apiKey = "8z8a7tupn5hubhjxqh8ubuz7";
$sharedSecret = "jsSJq2msbU";
$signature = hash("sha256", $apiKey.$sharedSecret.time());

$endpoint = "https://api.test.hotelbeds.com/activity-api/3.0/activities";

$request = new \http\Client\Request("POST",
    $endpoint,
    [ "Api-Key"     => $apiKey,
    "X-Signature" => $signature,
    "Accept"      => "application/json" ,
    ]);


$client = new \http\Client;

$client->enqueue($request)->send();

$response = $client->getResponse();

    echo "<pre>";
    print_r($response->getBody());
    echo "</pre>";

api 说

The available filters for the search is listed below.

It contains an array of filter with the following structure:

[{"searchFilterItems": [{"type": "destination", "value": "BCN"}]}]

The Object “searchFilterItems” contains the following attributes: type > and value.

The following examples illustrate the different types and values for > each filter:

Country

{"type": "country", "value": "PT"}

最佳答案

我遇到了同样的问题,花了我一段时间才弄明白。原来你需要使用 Body 类来表示帖子数据。

$msg = new http\Message\Body();
$msg->addForm([
  'field1' => 'value',
  'field2' => 'value2'
]);

$headers = null;

$request = new http\Client\Request('POST', 'https://example.com', $headers, $msg);

$client = new http\Client();
$client->enqueue($request);
$client->send();

$response = $client->getResponse();

在 Message 和 Body 类中还有一些方法可以用于包含文件等。

关于httprequest - 我如何在 pecl_http 库中使用发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37511255/

相关文章:

android - 在android中通过http post方法发送json对象

node.js - HTTP请求流程图

c# - 无法从网页下载源

php - 无法解释的 Gearman 超时之谜

php - 在 XAMPP (OS X/El Capitan) 上安装 MongoDB 驱动程序时出错

javascript - q-io : how to read response. 使用 Promises 的主体 JSON 对象?

javascript - 如何在 JavaScript 中强制程序等待 HTTP 请求完成?

php - 为 PHP 5.5.9 安装旧版本的 Yaml (1.3.1)

php - sudo pecl 在 os x lion 上安装 apc 错误

phpize 不能在 Mac OS X Lion 上运行