从iOS到PHP脚本的JSON数据 [英] JSON-data from iOS into PHP-script
本文介绍了从iOS到PHP脚本的JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在php脚本中访问json数据,它是通过http-post收到的?
我在iOS端执行以下操作:
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:
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];
如何在php脚本中访问这些数据?换句话说,当我在php脚本中调用 json_decode(...)
时, ...
?
How do I access this data in the php-script? In other words, when I call json_decode(...)
in the php-script, what is ...
?
推荐答案
如果您使用POST方法发送JSON,可以使用以下代码在PHP中接收
If your are sending your JSON in POST method , It can be received in PHP with the below code
<?php $handle = fopen('php://input','r');
$jsonInput = fgets($handle);
// Decoding JSON into an Array
$decoded = json_decode($jsonInput,true);
?>
这篇关于从iOS到PHP脚本的JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文