PDA

View Full Version : how can i get json by PHP ?



masoud_tamizy
2 Feb 2012, 5:22 AM
i send this JSON from my row-editor grid throuth CRUD (Ajax Proxy):
{"users":{"national_code":"1234567890","user_id":0,"first_name":"\u0646\u0627\u0645","last_name":"\u0641\u0627\u0645\u064a\u0644","father_name":"\u0646\u0627\u0645 \u067e\u062f\u0631","sh_sh":"","sh_s_sh":"","gender":"\u0645\u0630\u06a9\u0631","birth_date":null,"birth_location":"","email":"","home_address":"\u0622\u062f\u0631\u0633 \u0645\u0646\u0632\u0644","home_phone":"1234567890","postal_code":"1234567890","job_title":"","job_address":"","job_phone":"","mobile":"","fax":"","comment":""}}

how can i get it and what response i have to send (to client) for create operation ?

bseddon
2 Feb 2012, 5:53 AM
You can use the Direct or REST API. There are examples of using both techniques in the Examples that come with ExtJS distributions (the 'examples' sub folder). There are three examples: one using the Direct API (the 'direct' folder) and two using REST (the 'restful' and 'writer' folders). All the examples use PHP to process requests and respond correctly by returning JSON strings.

The APIs are documented and example, generic servers for the Direct API are available on the ExtJS download page (http://www.sencha.com/products/extjs/download?page=a) in the ExtDirect pack. The pack contains PHP, .NET and Ruby examples.

masoud_tamizy
2 Feb 2012, 6:20 AM
i only want know how can i recieve it on server ...

bseddon
2 Feb 2012, 6:38 AM
Maybe I'm not understanding your question. Is it that you want to send that exact string? Or is it that you want to send a JSON string in a way that cannot be modelled using ExtJS models and stores so the built-in features will not work?

masoud_tamizy
2 Feb 2012, 6:48 AM
Maybe I'm not understanding your question. Is it that you want to send that exact string? Or is it that you want to send a JSON string in a way that cannot be modelled using ExtJS models and stores so the built-in features will not work?
i sent that json but i'm not able to get it on server side .
i test $_Requst['users'] , but it didn't work !

bseddon
2 Feb 2012, 7:07 AM
I think you will need to post some example code (ExtJS and PHP) but first, are you using something like Firebug and, if so, do you see the request leaving the browser? Is there anything in the console? Do you know the request is received on the server? Do the Ext JS examples work? That is, is your server host PHP correctly?

Is there any other information you can provide that might be useful?

masoud_tamizy
2 Feb 2012, 7:17 AM
I think you will need to post some example code (ExtJS and PHP) but first, are you using something like Firebug and, if so, do you see the request leaving the browser? Is there anything in the console? Do you know the request is received on the server? Do the Ext JS examples work? That is, is your server host PHP correctly?

Is there any other information you can provide that might be useful?
i have all of them . i got that json from firebug .
when i send that request to server , i cann't get it on server side . and i can send anything in response . i see it on firebug .

byfirat
2 Feb 2012, 7:41 AM
$anArray = json_decode( $_POST[ 'users' ], true );

masoud_tamizy
2 Feb 2012, 7:57 AM
$anArray = json_decode( $_POST[ 'users' ], true );
thanks , but i encountered this error on response(in firebug) :

<br /><b>Notice</b>: Undefined index: users in <b>C:\wamp\www\Banking\PHP\user_real\create.php</b> on line <b>5</b><br />

mminnie
2 Feb 2012, 8:27 AM
Are you talking about reading the JSON data?


$rest_json = file_get_contents("php://input");
$rest_vars = json_decode($rest_json, true);

masoud_tamizy
2 Feb 2012, 8:39 AM
Are you talking about reading the JSON data?


$rest_json = file_get_contents("php://input");
$rest_vars = json_decode($rest_json, true);

this PHP command get a file and returns all of contents . but i want to recieve json data dynamically from client . please look all of above ...
thanks

mminnie
2 Feb 2012, 9:13 AM
I have read the above. I'm understanding your issue as there is no JSON data in the $_POST var. Correct?

This blog entry helped me solve this issue on my project.

http://blog.hao909.com/extjs4-read-posting-json-data-in-php/

mminnie
2 Feb 2012, 10:25 AM
The Content-Type in the Request packet is application/json. The JSON data in the HTTP Body part indicates it is not URL name and value pairs. In order to access that JSON string, you can use the code I posted in the previous post.

byfirat
2 Feb 2012, 12:19 PM
your text which you send to server must be look like
[{"id":1,"no":"5","text":"untitle","process":"somethink"} ]

bseddon
2 Feb 2012, 2:37 PM
You need to take a look at the ExtJS examples. Here are worked examples of reading the JSON from an ExtJS post in PHP. For example:

extjs\examples\direct\php\router.php

you will find this code:



if(isset($HTTP_RAW_POST_DATA)){
header('Content-Type: text/javascript');
$data = json_decode($HTTP_RAW_POST_DATA);
}else if(isset($_POST['extAction'])){ // form post
$isForm = true;
$isUpload = $_POST['extUpload'] == 'true';
$data = new BogusAction();
$data->action = $_POST['extAction'];
$data->method = $_POST['extMethod'];
$data->tid = isset($_POST['extTID']) ? $_POST['extTID'] : null; // not set for upload
$data->data = array($_POST, $_FILES);
}else{
die('Invalid request.');
}


The relevant line is probably: "$data = json_decode($HTTP_RAW_POST_DATA);"

However, if you configure your web server to run the examples you will be able set a break point in your favorite PHP IDE and examine the PHP variables.

In the REST example (extjs\examples\writer\remote\lib\request.php) it's different because there are different ways the request can appear at the server. I think you need to work through these examples and them apply that learning to your specific problem.


if ($this->method == 'PUT') { // <-- Have to jump through hoops to get PUT data $raw = '';
$httpContent = fopen('php://input', 'r');
while ($kb = fread($httpContent, 1024)) {
$raw .= $kb;
}
fclose($httpContent);
$params = array();
parse_str($raw, $params);


if (isset($params['data'])) {
$this->params = json_decode(stripslashes($params['data']));
} else {
$params = json_decode(stripslashes($raw));
if ($params)
$this->params = $this->isRestful() ? $params : $params->data;
}
}
else
{
// grab JSON data if there...
$this->params = (isset($_REQUEST['data'])) ? json_decode(stripslashes($_REQUEST['data'])) : null;


if (isset($_REQUEST['data'])) {
$this->params = json_decode(stripslashes($_REQUEST['data']));
} else {
$raw = '';
$httpContent = fopen('php://input', 'r');
while ($kb = fread($httpContent, 1024)) {
$raw .= $kb;
}
$params = json_decode(stripslashes($raw));
if ($params) {
$this->params = $this->isRestful() ? $params : $params->data;
}
}
}

masoud_tamizy
3 Feb 2012, 12:05 AM
thanks every one who help me =D>