How can I access json data within a php-script, which it received via http-post? I'm doing the following on the iOS-side:
如何在一个php脚本中访问json数据?我在ios方面做以下事情:
NSData *data = [NSJSONSerialization dataWithJSONObject:object options:0 error:NULL];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com/script.php"]];
[req setHTTPMethod:@"POST"];
[req setHTTPBody:data];
[NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&err];
How do I access this data in the php-script? In other words, when I call json_decode(...)
in the php-script, what is ...
?
如何在php脚本中访问这些数据?换句话说,当我在php脚本中调用json_decode(…)时,什么是…?
4 个解决方案
#1
14
If your are sending your JSON in POST method , It can be received in PHP with the below code
如果您正在用POST方法发送JSON,可以用PHP接收,代码如下
<?php $handle = fopen('php://input','r');
$jsonInput = fgets($handle);
// Decoding JSON into an Array
$decoded = json_decode($jsonInput,true);
?>
#2
1
The post request when sent using iOS does not works with $_POST. If similar request is issued using js the json in post does works.
使用iOS发送的post请求不能使用$_POST。如果使用js发出类似的请求,则post中的json有效。
#3
0
In the php script you take the POST data and do
在php脚本中,您获取POST数据并执行。
json_decode($data);
and that will give you an object you can work with.
这会给你一个你可以使用的对象。
#4
0
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody: jsonData];
[request setValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];
This code can be used to send a response
此代码可用于发送响应
#1
14
If your are sending your JSON in POST method , It can be received in PHP with the below code
如果您正在用POST方法发送JSON,可以用PHP接收,代码如下
<?php $handle = fopen('php://input','r');
$jsonInput = fgets($handle);
// Decoding JSON into an Array
$decoded = json_decode($jsonInput,true);
?>
#2
1
The post request when sent using iOS does not works with $_POST. If similar request is issued using js the json in post does works.
使用iOS发送的post请求不能使用$_POST。如果使用js发出类似的请求,则post中的json有效。
#3
0
In the php script you take the POST data and do
在php脚本中,您获取POST数据并执行。
json_decode($data);
and that will give you an object you can work with.
这会给你一个你可以使用的对象。
#4
0
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody: jsonData];
[request setValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];
This code can be used to send a response
此代码可用于发送响应