2016-05-29 108 views
1

hotelbeds api如何使用POST請求在pecl_http庫

使用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稱

以下篩選可用於搜索是下面列出。

它包含過濾器的具有以下結構的陣列:

[{ 「searchFilterItems」:[{ 「類型」: 「目的地」, 「值」: 「BCN」}]}]

對象「searchFilterItems」包含以下屬性:type>和value。

以下實施例說明了不同類型和值>每個過濾器:

國家

{ 「類型」: 「國家」, 「值」: 「PT」}

回答

2

我遇到了同樣的問題,花了一點時間才弄明白。原來你需要使用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(); 

有一些在消息美體類提供更多的方法,包括文件等

1

嘗試這種方式

$request = new http\Client\Request; 
      $body = new http\Message\Body; 
      $body->append('{Your JSON}'); 
      $request->setRequestUrl('https://api.hotelbeds.com/hotel-api/1.0 hotels'); 
      $request->setRequestMethod('POST'); 
      $request->setBody($body); 
      $request->setHeaders(array(
       'Accept'      => 'application/json', 
       'Content-Type'   => 'application/json', 
       'Api-Key'    => $owapiKey, 
       'X-Signature'    => $signature, 
       //'Accept-Encoding' => 'Gzip', //Deflate 
       'cache-control'  => 'no-cache' 
      )); 

      try { 
       $client = new http\Client; 
       $client->enqueue($request)->send(); 
       $response = $client->getResponse(); 

       if ($response->getResponseCode() != 200) { 
       echo("HTTP CONNECT FAILURE: -> ". 
       $response->getTransferInfo("effective_url"). 
       $response->getInfo().$response->getResponseCode()); 
      } else { 
          $res=$response->getBody()->toString(); 
         }    

      } catch (Exception $ex) { echo("Error while sending request, reason: %s\n".$ex->getMessage()); }