You are here: Re: How does one handle JSON strings from the browser « PHP Programming Language « IT news, forums, messages
Re: How does one handle JSON strings from the browser

Posted by seth on 08/26/07 23:07

On Aug 26, 11:18 am, seth <satyen.sh...@gmail.com> wrote:
> Hi:
>
> I'm trying to read JSON strings sent from the browser. Here is the
> scenario:
>
> 1. Using YUI tookit
> 2. sending JSON string from YUI toolkit - using the provided
> asyncRequest method.
>
> **I would like to read the JSON string sent from the client on the
> server side.
>
> Here is the Javascript which show: [this is a code snippet only]
> 1. creating an object
> 2. encoding to JSON string
> 3. making the ajax request.
>
> -----------------------------------------------------------------------------------------------------------------
> 12 function makeRequest() {
> 13 var jsonObj;
> 14 var transaction;
> 15 var jstr;
> 16
> 17 jsonObj = {
> 18 action : "ack",
> 19 var1 : "fly"
> 20 };
> 21
> 22 jstr = jsonObj.toJSONString();
> 23 transaction =
> YAHOO.util.Connect.asyncRequest('POST', "testPost.php",
> responseHandler, jstr);
> 24 };
> -----------------------------------------------------------------------------------------------------------------
>
> * line 17 makes the object
> * line 22 converts to a json string
> * line 23 issues the ajax request
>
> As a comparison, this is how I deal with the request in Perl:
>
> -----------------------------------------------------------------------------------------------------------------
> 1 #!/usr/bin/perl
> 2
> 3 use strict;
> 4 use JSON;
> 5
> 6
> 7 my $jsonObj = new JSON;
> 8 my $buffer;
> 9 my $param;
> 10 my $jsonStr = {};
> 11
> 12 print "Content-type: text/html\n\n";
> 13
> 14 read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
> 15
> 16 if (defined $buffer && $buffer ne "") {
> 17 $param = $jsonObj->jsonToObj($buffer);
> 18 }
> -----------------------------------------------------------------------------------------------------------------
>
> * line 14 does a read from STDIN
> * line 17 converts the JSON string to a hash in Perl
>
> 1. how do I do something similar in PHP?
> 2. I found that can get the CONTENT_LENGTH from $_SERVER
> 3. I found that $_POST doesn't give me anything valid - as expected.
> 4. Doing a "$myInput = fopen('php://stdin', 'r')" doesn't help
> either
>
> Can someone please point me in the right direction to recover the
> passed in JSON string from the client?
>
> thanks in advance.
> -S

I found what I needed. Maybe it will help others?

Idea was to find a way to handle RAW data in PHP.

There are 2 ways:

1. enable $HTTP_RAW_POST_DATA
2. use PHP input/output streams - 'php://input'

It's cited here:
http://www.php.net/manual/en/wrappers.php.php

Here is my sample code which works great!

1 <?php
2
3 header('Content-type: text/html');
4 $data = file_get_contents('php://input', 'r');
5 print ($data);
6 ?>
7

Here is the output:
===============================

Send Ajax Request
Got Data! ...
Transaction id: 12
HTTP status: 200
Status code message: OK
# HTTP headers:

Date: Sun, 26 Aug 2007 21:11:08 GMT Server: Apache X-Powered-By:
PHP/5.2.1 Content-Length: 30 Keep-Alive: timeout=15, max=99
Connection: Keep-Alive Content-Type: text/html; charset=UTF-8


PHP response: {"action":"ack","var1":"fly"} <<<<=============
[recovered JSON string]
Argument object: undefined

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация