使用ASIFormDataRequest将json发送到php [英] Send json to php using ASIFormDataRequest

查看:101
本文介绍了使用ASIFormDataRequest将json发送到php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iPhone的新手,我正在尝试将NSMutable数组转换为json字符串然后使用请求将此字符串发送到php文件,然后使用响应NSLog再次打印它以确保它已成功发送。所以我在viewDidLoad中编写了以下代码

I'm new in iPhone and I'm trying to convert NSMutable array to json string then send this string to php file using request, and then print it again using respond to NSLog to ensure that it has been send successfully. So I wrote the following code in viewDidLoad

NSString *phpUrl = @"http://dt-works.com/eman/bookMarks.php";

NSMutableArray *arrayKey = [[NSMutableArray alloc] initWithObjects:@"one", @"two", @"three", nil];
NSMutableArray *arrayValue1 = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", nil]; 
NSMutableArray *arrayValue2 = [[NSMutableArray alloc] initWithObjects:@"11", @"22", @"33", nil]; 

NSDictionary *theReqDictionary1 = [NSDictionary dictionaryWithObjects:arrayValue1 forKeys:arrayKey];
NSDictionary *theReqDictionary2 = [NSDictionary dictionaryWithObjects:arrayValue2 forKeys:arrayKey];

NSMutableArray *myArray = [NSMutableArray arrayWithObjects:theReqDictionary1,theReqDictionary2, nil];

NSString *jsonString = [myArray JSONRepresentation];

NSLog(@"%@", jsonString);



 NSDictionary *questions = nil;
NSURL *link = [NSURL URLWithString:[phpUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:link];
[request setPostValue:jsonString forKey:@"jsonString"];
[request setTimeOutSeconds:120];
[request setDelegate:self];
NSError *error = [request2 error];
[request2 startSynchronous];

if (!error) { 
    NSData *response = [request responseData];
    NSString *json = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    questions = [json objectFromJSONString];
    NSLog(@"Data: %@", questions);

} 

,bookMarks.php中的代码为:

and the code in bookMarks.php is:

$handle = fopen('php://input','r');
$jsonInput = $_POST['jsonString'];
print_r($jsonInput);

但它给了我:

[{"one":"1","two":"2","three":"3"},{"one":"11","two":"22","three":"33"}]
Data: (null)

和我希望数据为:[{one:1,two:2,three:3},{one:11,two:22 ,三:33}]
怎么做?

and I want Data to be as: [{"one":"1","two":"2","three":"3"},{"one":"11","two":"22","three":"33"}] how to do this??

推荐答案

试试这个,而不是使用密钥发布JSON字符串,您需要将其作为数据发布到您的脚本中以便读入。目前在您的脚本中,您甚至不使用$ handle ...

Try this, instead of posting the JSON string with a key, you need to post it as Data to your script to be read in. Currently in your script you aren't even using $handle for anything...

NSURL *link = [NSURL URLWithString:[phpUrl     stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:link];
[request appendPostData:[jsonString  dataUsingEncoding:NSUTF8StringEncoding]]; 
[request setTimeOutSeconds:120];
[request setDelegate:self];

和你的PHP代码..

And your php code..

$handle = fopen('php://input','r');
$jsonInput = fgets($handle);
$decoded = json_decode($jsonInput,true);
print_r($decoded);

这篇关于使用ASIFormDataRequest将json发送到php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆