How to read chunked response?

Discussion in 'PHP' started by prashcom, Oct 26, 2007.

  1. #1
    Hi
    I am working on amazon and what they are doing is if i am doing any request in curl then they are sending the response as chunked data.

    Now my problem is how i will read this data. I can read this data but not able to completely parse it..

    This is my code :

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url); // set URL
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return result in a variable
    curl_setopt($ch, CURLOPT_HEADER, 1); // include headers in result
    curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheaders); // send my headers
    $result = curl_exec($ch);
    //get the default response headers
    $headers = curl_getinfo($ch);
    //print_r($headers);
    curl_close($ch);

    and this is my response

    HTTP/1.1 200 OK
    x-amzn-request-id: a77f37e2-2247-4aba-8d39-54da9e831ea6
    Content-Type: application/octet-stream
    Transfer-Encoding: chunked
    Date: Fri, 26 Oct 2007 17:46:24 GMT
    Server: MFA Query Service

    and this is the actual content what i am trying to filter :

    order-id order-item-id purchase-date payments-date buyer-email buyer-name buyer-phone-number sku product-name quantity-purchased currency item-price item-tax shipping-price shipping-tax ship-service-level recipient-name ship-address-1 ship-address-2 ship-address-3 ship-city ship-state ship-postal-code ship-country ship-phone-number delivery-start-date delivery-end-date delivery-time-zone delivery-Instructions
    203-7894296-3757945 07802496435979 2007-10-25T06:46:25-07:00 2007-10-25T06:46:25-07:00 dead@btinternet.com simon brown 02082913628 B000T5N0JE Advanced Mouse Trap 3 GBP 5.85 0.00 5.50 0.00 Standard simon brown 166 kilmorie road forest hill london london se232sr GB 02082913628


    so i need this content in name->value pair and some where i am doing mistake to read the content properly..

    Will any one pls guide me what needs to do here?

    Thanks
    Prashant Agarwal
    http://prashcom.blogspot.com
     
    prashcom, Oct 26, 2007 IP
  2. gota

    gota Peon

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Something like....

    <?php
    
    $url = '';
    
    $io = curl_init ();
    
    curl_setopt ( $io, CURLOPT_URL, $url );
    
    curl_setopt ( $io, CURLOPT_TIMEOUT, 4 );
    
    curl_setopt ( $io, CURLOPT_ENCODING, '' );
    
    curl_setopt ( $io, CURLOPT_MAXREDIRS, 3 );
    
    curl_setopt ( $io, CURLOPT_FOLLOWLOCATION, true );
    
    curl_setopt ( $io, CURLOPT_RETURNTRANSFER, true );
    
    curl_setopt ( $io, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)' );
    
    curl_setopt ( $io, CURLOPT_HEADER, false );
    
    $out = trim ( curl_exec ( $io ), "\r\n" );
    
    curl_close ( $io );
    
    $out = preg_split ( "/\r?\n/", $out );
    
    $out = array_combine ( explode ( "\t", $out[0] ), explode ( "\t", $out[1] ) );
    
    print_r ( $out );
    
    ?>
    PHP:
    $out will contain an array with key name equals value! IE...

    Array
    (
        [order-id] => 203-7894296-3757945
        [order-item-id] => 07802496435979
        [purchase-date] => 2007-10-25T06:46:25-07:00
        [payments-date] => 2007-10-25T06:46:25-07:00
        [buyer-email] => dead@btinternet.com
        [buyer-name] => simon brown
        [buyer-phone-number] => 02082913628
        [sku] => B000T5N0JE
        [product-name] => Advanced Mouse Trap
        [quantity-purchased] => 3
        [currency] => GBP
        [item-price] => 5.85
        [item-tax] => 0.00
        [shipping-price] => 5.50
        [shipping-tax] => 0.00
        [ship-service-level] => Standard
        [recipient-name] => simon brown
        [ship-address-1] => 166 kilmorie road
        [ship-address-2] => forest hill
        [ship-address-3] => 
        [ship-city] => london
        [ship-state] => london
        [ship-postal-code] => se232sr
        [ship-country] => GB
        [ship-phone-number] => 02082913628
        [delivery-start-date] => 
        [delivery-end-date] => 
        [delivery-time-zone] => 
        [delivery-Instructions] => 
    )
    Code (markup):
     
    gota, Oct 26, 2007 IP