2017-09-25 30 views
0

這裏下面我重視我的API請求hotelbed API HTTP錯誤

$apiKey = "XXXX"; 
$Secret = "XXX"; 
$endpoint = "https://api.test.hotelbeds.com/hotel-api/1.0/hotels"; 
$request = new http\Client\Request("POST", 
$endpoint, 
[ "Api-Key"  => $apiKey, 
    "X-Signature" => $signature, 
    "Accept"  => "application/xml" ]); 
try 
{ $client = new http\Client; 
$client->enqueue($request)->send(); 
$response = $client->getResponse(); 
if ($response->getResponseCode() != 200) { 
    printf("%s returned '%s' (%d)\n", 

     $response->getTransferInfo("effective_url"), 

     $response->getInfo(), 
     $response->getResponseCode() 
    ); 
} else { 
    printf($response->getBody()); 
} 
} catch (Exception $ex) { 
printf("Error while sending request, reason: %s\n",$ex->getMessage()); 
}' 

得到以下錯誤

Uncaught Error: Class 'http\Client\Request' not found in

+0

從終端的EOL中刪除''' –

回答

0

您需要添加使用聲明。

use http\Client\Request; 

$request = new Request(blah blah); 

當然我假設你使用的是Composer自動加載器。如果沒有,您還需要require_once()該文件。

+0

我沒有使用任何Composer自動加載器。從api提供商那裏得到這段代碼 –

+0

如果你不使用自動加載,你需要手動要求所有你將要使用的類文件。 'require_once'/path/to/Request.php;使用http \ Client \ Request;' – delboy1978uk

0

您可以嘗試使用cURL而不是pecl_http。這裏有一個例子:

<?php 

// Your API Key and secret 
$apiKey = "yourApiKey"; 
$Secret = "yourSecret"; 

// Signature is generated by SHA256 (Api-Key + Secret + Timestamp (in seconds)) 
$signature = hash("sha256", $apiKey.$Secret.time()); 

$endpoint = "https://api.test.hotelbeds.com/hotel-api/1.0/status"; 

echo "Your API Key is: " . $apiKey . "<br>"; 
echo "Your X-Signature is: " . $signature . "<br><br>"; 

// Example of call to the API 
try 
{ 
    // Get cURL resource 
    $curl = curl_init(); 
    // Set some options - we are passing in a useragent too here 
    curl_setopt_array($curl, array(
     CURLOPT_RETURNTRANSFER => 1, 
     CURLOPT_URL => $endpoint, 
     CURLOPT_HTTPHEADER => ['Accept:application/json' , 'Api-key:'.$apiKey.'', 'X-Signature:'.$signature.''] 
    )); 
    // Send the request & save response to $resp 
    $resp = curl_exec($curl); 

    // Check HTTP status code 
    if (!curl_errno($curl)) { 
     switch ($http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE)) { 
     case 200: # OK 
      echo "Server JSON Response:<br>" . $resp; 
      break; 
     default: 
      echo 'Unexpected HTTP code: ', $http_code, "\n"; 
      echo $resp; 
     } 
    } 

    // Close request to clear up some resources 
    curl_close($curl); 


} catch (Exception $ex) { 

    printf("Error while sending request, reason: %s\n",$ex->getMessage()); 

} 
?> 

只要確保你首先取消註釋;延長= php_curl.dll在php.ini文件中,並重新啓動服務器。 我們計劃在開發者平臺上更新我們的示例,因爲有些已過時或沒有很好的文檔記錄。