I'm trying to build myself a small JSON-RPC server using PHP.

Using wireshark, here's the conversation:

        POST /.../service.php?nocache=1297004648751 HTTP/1.1
        User-Agent: Opera/9.80 (X11; Linux i686; U; en) Presto/2.7.62 
        Host: localhost
        Accept: text/html, application/xml;q=0.9, application/xhtml+xml,
image/png, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1
        Accept-Language: en-US,en;q=0.9
        Accept-Charset: iso-8859-1, utf-8, utf-16, *;q=0.1
        Accept-Encoding: deflate, gzip, x-gzip, identity, *;q=0
        Referer: http://localhost/ssd/php/testrpc/build/
        Connection: Keep-Alive, TE
        TE: deflate, gzip, chunked, identity, trailers
        Content-Length: 80
        Content-Type: application/json
        Pragma: no-cache
        Cache-Control: no-cache
        X-Qooxdoo-Response-Type: application/json
        Content-Transfer-Encoding: binary

 said it, Bush junior proved it
        HTTP/1.1 200 OK
        Date: Sun, 06 Feb 2011 15:04:08 GMT
        Server: Apache/2.2.14 (Ubuntu)
        Accept-Ranges: bytes
        X-Powered-By: PHP/5.3.2-1ubuntu4.7
        Keep-Alive: timeout=15, max=100
        Connection: Keep-Alive
        Transfer-Encoding: chunked
        Content-Type: application/json; charset=UTF-8


The code to handle the request is:


        $request = json_decode($GLOBALS['HTTP_RAW_POST_DATA']);

        $response = (object) array(
                "id" => $request->id,
                "result" => $request,
                "error" => null

        header("Content-Type: application/json; charset=UTF-8", true);
        print json_encode($response);


The "6f" above is the length of the output in hex (I tried with
various lengths of the string, it is always correct), the 0 at the end
is probably some C-like string termination mark.

Now, it seems the client (some JavaScript running in Firefox) has no
problem decoding the answer, in spite of the hex length placed at the
beginning. However, I can't rely on browsers playing nicely with
incorrect JSON, so I would very much like to generate an output
without the length of the response written as hex at the beginning,
and without the terminating 0. What am I doing wrong? Why does the
length of the string get written?

I tried concatenation of an empty string at the beginning and at the
end, supposing that for some reason json_encode() doesn't produce a
plain string, and hoping that concatenating it to a proper string
would produce a plain string, but it didn't help either.

Before posting to the list, I tried searching for the problem on the
web, and also experimented by outputting plain, hand-written strings.
It didn't really help. Maybe it's a setup problem? My problem is, php
is absolutely new to me, so I don't even know how to start diagnosing
the problem (I started experimenting just a few hours ago).

To decide whether it's a setup problem, here's the development platform:
        OS: Kubuntu 10.04.1 LTS
        Apache: Apache/2.2.14 (Ubuntu)
        PHP: 1.0.5-dev
        json version: 1.2.1

I don't think the browser is relevant, since it behaves the same in
Opera and Firefox, and it doesn't happen in the browser, it happens on
the server, since that's what wireshark shows.



In politics, stupidity is not a handicap. (Napoleon)

PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to