0

I am echoing json_encoded data from one php script to another (the request is made by fsockopen/GET).

When having encoded an array with 40 elements, there is no problem. When doing exactly the same thing with 41, some numbers and \r\n is added to the beginning of the json string.

This is the beginning of the string just before I echo it:

{"transactions":[{"transaction_id":"03U191739F337671L",

This is how I send the data:

header('Content-Type: text/plain; charset=utf-8');
error_log(json_encode($transaction_list));
echo json_encode($transaction_list);

As soon as I have received the data in the requesting script I print it again to error_log:

27fc\r\n{"transactions":[{"transaction_id":"03U191739F337671L",

The "27fc\r\n" is not there if I retrieve less data.

This is how I handle the response:

$response="";
 while (!feof($fp)) {
            $response .= fgets($fp, 128);
 }

 //Seperate header and content
 $separator_position = strpos($response,"\r\n\r\n");      

 $header_text = substr($response,0,$separator_position);      

 $body = substr($response,$separator_position+4);                   
 error_log($body);  

 fclose($fp);   

I have tried playing around with the time out of the fsockopen request, that doesn't matter. The same thing with max_execution_time and max_input_time in php.ini, doesn't matter. I was thinking that the content in some way may have been cut due to time out...

The 41st array is having no different format of the content than the preceding ones.

What is happening and how can I fix it?

I am using Linux, Apache (httpd) and PHP.

UPDATE

The data seems to be chunked. In the response, following header is included: "Transfer-Encoding: chunked".

Nicsoft
  • 3,644
  • 9
  • 41
  • 70
  • 1
    Kinda smells like something from [chunked transfer encoding](http://en.wikipedia.org/wiki/Chunked_transfer_encoding). Is that JSON string 10,236 octets long? Are you or something else in the output stream invoking chunked output? – Charles Dec 08 '12 at 10:28
  • Probably not related, but why are you sending JSON with the wrong content type? The content type of JSON is `application/json`, not `text/plain`. – T.J. Crowder Dec 08 '12 at 10:29
  • @Charles, I am not invoking something intentionally. How is chunked output invoked? Could it automatically start chunking in case the size is larger than a predefined length? I have tried to find maximum response size in GET request but can only find information about size of POST... The size of the json_length before sending it is 8400 in length (how many octets is that?). I seems to be an annoyingly even number... – Nicsoft Dec 08 '12 at 10:42
  • @Nicsoft, an octet is 8 bits, or one byte. Sorry about that, wrong crowd. It's possible that you could get chunked transfer encoding if you're using [output buffering](http://php.net/function.ob-start). – Charles Dec 08 '12 at 10:45
  • I would use php *split* function (on a \r\n) (**not** the \r\n\r\n). your code now get the 1st pos of '\r\n\r\n' but then adds anther 4 (incl. the CR+LF). – Roger Dec 08 '12 at 10:54
  • Ok, thanks. Not using ob_start anywhere in my project. I made some test and I am pretty sure the problem starts when the length of the json-string exceeds (around?) 8000. If the length is 7993 it works, when it is 8198 it doesn't... – Nicsoft Dec 08 '12 at 10:56
  • Printing everything on the requesting sice, I can see this: "Transfer-Encoding: chunked" So you are correct @Charles. How do I handle the response in this case? Updating my question with this informaiton. – Nicsoft Dec 08 '12 at 11:07
  • I vaguely recall that PHP *itself* has about an 8k buffer before it shoves data at the web server. What are you using that sees the hex characters? All modern browsers support chunked encoding... – Charles Dec 08 '12 at 11:11
  • I am requesting data from one PHP script to another. Just printing to a log file. I think I need to unchunk it myself. Trying to read up on the exact format of chunked data now. What does this 8k mean, I cannot get any more information that those 8k? – Nicsoft Dec 08 '12 at 11:23
  • 1
    What do you get if you use `file_get_contents` to read the file? – Salman A Dec 08 '12 at 11:25
  • @Salman, I actually need to send data in order to get the response (some data used for making the selection of what to return).But if I don't care about that it is the correct data returned, it actually seems to be working... – Nicsoft Dec 08 '12 at 11:38

1 Answers1

0

Based on @Salmans idea of using file_get_contents, this is the working solution. This uses POST to send the data (GET didn't seem to be working, I think one has to append that query string to the URL oneself):

$postdata = http_build_query(         
   array('customer_id' => $customer_id)
);

$opts = array('http' =>
   array(
    'method'  => 'POST',
    'header'  => 'Content-type: application/x-www-form-urlencoded',
    'content' => $postdata
   )
);

$context  = stream_context_create($opts);  
$content = file_get_contents($my_url, false, $context);
return $content;
Nicsoft
  • 3,644
  • 9
  • 41
  • 70