2016-08-02 156 views
0

最近,我开始Hotelbeds APITUDE PHP API如何从hotelbeds apitude PHP API的gzip编码内容

工作我想通过pecl_http

现在我面临的一些问题,让gzip编码发送请求并得到响应数据通过API。以下是端点和头information

我想用下面的代码 -

$xml_part = <<< EOD 
       <availabilityRQ xmlns="http://www.hotelbeds.com/schemas/messages" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" dailyRate="true"> 
       <stay checkIn="2016-09-15" checkOut="2016-09-16"/> 
       <occupancies> 
       <occupancy rooms="1" adults="2" children="0"> 
       <paxes> 
       <pax type="AD"/> 
       <pax type="AD"/> 
       </paxes> 
       </occupancy> 
       </occupancies> 
       <hotels> 
       <hotel>1067</hotel> 
       <hotel>1070</hotel> 
       </hotels> 
       <keywords> 
       <keyword>34</keyword> 
       <keyword>38</keyword> 
       <keyword>100</keyword> 
       </keywords> 
       <boards included="true"> 
       <board>RO</board> 
       <board>BB</board> 
       </boards> 
       <rooms included="TRUE"> 
       <room>DBT.ST</room> 
       </rooms> 
       <accommodations> 
       <accommodation>HOTEL</accommodation> 
       <accommodation>HOSTEL</accommodation> 
       </accommodations> 
       <reviews> 
       <review type="TRIPADVISOR" maxRate="5" minReviewCount="3"/> 
       </reviews> 
       <filter minRate="100.000" maxRate="170.000"/> 
       <filter minCategory="3" maxCategory="5"/> 
       <filter paymentType="AT_HOTEL"/> 
       <filter maxRatesPerRoom="3"/> 
       <filter packaging="TRUE"/> 
       <filter hotelPackage="YES"/> 
       <filter maxRooms="2"/> 
       </availabilityRQ> EOD; 

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

       $body = new http\Message\Body(); 

       $body->append($xml_part); 

       $request = new http\Client\Request("POST", 
        $endpoint, 
        ["Api-Key" => $hotel_beds_config['api_key'], 
         "X-Signature" => $signature, 
         "Content-Type" => "application/xml", 
         "Accept" => "application/xml", 
         "Accept-encoding" => "Gzip" 
        ], 
        $body 
       ); 

       try { 

        $client = new http\Client; 

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

        $response = $client->getResponse(); 

        if ($response->getResponseCode() != 200) { 

         printf($response->getBody()); 

        } else { 
         echo '<pre>'; 
         printf(json_encode($response->getBody())); 
         echo gzencode(json_encode($response->getBody())); 
         echo '</pre>'; 

        } 

       } catch (Exception $ex) { 

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

       } 

我得到200响应代码做一个API请求时。我所面临的所有问题都是从响应主体中检索数据。在输出我看到类似下面的编码数据 -

{}℃

我怎样才能获得的gzip编码的数据作为请求内容主体?

回答

0

我不熟悉pecl_http但我看到两个问题与您的代码:

这条线:

</availabilityRQ> EOD; 

应与

</availabilityRQ> 
EOD; 

将被替换结尾分隔符EOD;必须自己在一行;没有其他字符,甚至在它之前或之后都不允许有空格。

2.如果你得到的回应是正确的GZIP编码,然后将这些线都没有多大意义:

printf(json_encode($response->getBody())); 
echo gzencode(json_encode($response->getBody())); 

你永远不要试图响应解码;其实你试图json_encode吧!试试

$gz_encoded = $response->getBody(); 
$gz_decoded = gzdecode($gz_encoded); 

现在您可以检查结果。如果它是一个JSON字符串,就可以对其进行解码:

$final_str = json_decode($gz_decoded); 
0

你手动设置“接受编码:gzip”头,所以你的反应可能是GZIP编码,因此前者的海报是正确的。

通过删除显式标题并告诉客户端使用hande gzip编码的内容来避免这种情况,使用$client->setOptions(["compress" => true]);