Search code examples
phpapp-store-connectgzipguzzle

AppStoreConnect, salesReports return handle in php


I am getting AppStoreConnect, salesReports via API request to it. As mentioned in the documentation the return type is Content-Type: gzip. What i get is the gzip blob of data and I don't know how do I process it in PHP.

The blob looks something like this =>

��]�n�8}�~�~ ���e?:���Ű��T�6� �H�$wѿ_ʒ/i"j(�F�"d�:�3gf��8��E̦�cm�4���_l,~��h#_��g�D�Q��y�]�����0��"L��@�kir��!߷b��K�=��2l���m�F/�/n�X��_��i)�:{9ֈ���3�l�_���K�q��/Q���R~<=}�|�-Y��&��"�5II�G���\f�g_2�n6Q���1}^�OQ�����L,��D"��{��w6z�/��8Sb��������-�l����r-^�01�J�đ�6�?

And when I unzip it using gzdecode() I get somewhat readable data, yet it looks mixed and I have no idea how to sort it. I need to a way to sort through these data.

$blob = $this->client->request(
                'GET',
                "salesReports",
                [
                    'headers' => [
                        'Accept'        => 'application/a-gzip, application/json',
                        'Accept-Encoding'   => 'deflate,gzip',
                        'Authorization' => "Bearer $this->jwt"
                    ],
                    'query' => $queryParams
                ]
            )->getBody()->getContents();

 $res=  gzdecode($blob) ;
 echo $res;

Solution

  • The converted blob is a space delimitated csv content, you can separate them by lines (/n) and columns (/t). I have no idea why apple didn't use comma delimitated format, it would have been very easy to identify.

    $lines =  explode("\n", gzdecode($res));
    
    foreach ($lines as $key => $line)
    {
       $cols = explode("\t", $line);
    }