使用iOS上的POST将数组传递给PHP [英] Passing array to PHP using POST from iOS

查看:403
本文介绍了使用iOS上的POST将数组传递给PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我已经浏览了无数类似的问题,但是没有一个人能完整地回答我所寻找的问题,也没有一个完整的答案,因此希望大家能为我提供帮助.

So I've looked through an endless number of like problems but none of them answered what I was looking for or answered it in a complete manner so hopefully you all can help me out.

我需要使用POST或将其正常运行的方式,将iOS上的restaurantID数组传递给PHP文件.我知道ASIHTTPRequest,但我正在寻找内置的东西,它已被开发人员放弃.最后,我不想通过URL传递它们,因为我不知道会有多少个条目.

I need to pass an array of restaurantID's from iOS to a PHP file using POST or anyway that would work well. I know about ASIHTTPRequest but I'm looking for something built in and it has been abandon by the developer. And lastly, I don't want to pass them through the URL because I don't know how many entries there will be.

这就是我到目前为止所得到的.

So here's what I got so far.

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:theURL]];

[request setHTTPMethod:@"POST"];

NSMutableDictionary *jsonDict = [[NSMutableDictionary alloc] init];
[jsonDict setValue:restaurants forKey:@"restIDs"];
NSLog(@"JSON Dict: %@",jsonDict);//Checking my array here


NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:kNilOptions error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"JSON String: %@",jsonString); //Checking my jsonString here...

[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

[request setValue:@"json" forHTTPHeaderField:@"Data-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: jsonData];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSLog(@"Return DATA contains: %@", [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableContainers error:nil]);
NSArray *restMenuCount = [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableContainers error:nil];

因此,从此结束,我已经检查了所有内容,一切看起来都不错,但是从PHP来看,它甚至都没有收拾它.

So, from this end, I've checked everything and it all looks good, but from the PHP end it doesn't even pick it up.

这是我的PHP文件的样子:

Here's what my PHP file looks like:

$restIDs = $_POST['restIDs'];
echo $restIDs; //Checking to see if it even has anything......but nothing there
for ($i = 0; $i < $restIDs.count; $i++) {
    $query = "SELECT * FROM `MenuItems` WHERE rest_id = '$restID'";
    $result = mysqli_query($connect, $query);
    $number = mysqli_num_rows($result);
    $menuNumber[$i] = $number;
}

echo json_encode($menuNumber);

最后,我在做什么错?为什么我的PHP端什么都没收到.最重要的是,有人可以向我解释如何通过POST发送数组.因为我觉得这是我真正的问题,所以我对它的了解不足以自己解决问题.我不明白如何从iOS端将所有内容放入PHP端.

So finally, what am I doing wrong? Why am I not receiving anything on my PHP end. And most of all, can someone explain to me how to send array's via a POST. Because I feel like that's my real problem here, I don't understand it enough to fix the problem myself. I don't understand how you can put everything in from the iOS side and pick it up on the PHP side.

我希望所有这些事情都足够清楚,谢谢.

I hope all of this was clear enough, thanks in advance.


我尝试将数组作为字符串通过URL传递,然后分解它,幸运的是它起作用了……但是我处于URL限制之下,所以我仍然想找出另一种解决方案.至少,现在我知道我的其余代码正在按预期工作.


I tried passing the array as a string through the URL then exploding it, luckily it worked...but I'm just under the URL limit, so I'd still like to figure out another solution. At least, now I know the rest of my code was working as expected.

推荐答案

您似乎对$_POST变量的填充方式有基本的误解.将文档类型指定为JSON,然后将JSON字符串作为后缀body不会自动填充此数组.主体必须采用特定格式.通常,这意味着使用url编码对,例如a=1&b=2&c=2&d=%2fg+y等.这在某种程度上限制了您可以发送的数据类型.特别是,如果要使其自动显示在$ _POST变量中,则从这个意义上讲,任意JSON对象都是不可能的.这里有一些选项:

You seem to have a basic misunderstanding of how the $_POST variable is filled in. Specifying the document type as JSON and throwing the JSON string in as the post body will not auto-populate this array. The body has to be in a specific format. Usually, this means url-encoded pairs, eg a=1&b=2&c=2&d=%2fg+y etc. This somewhat limits the kind of data you can send. In particular, an arbitrary JSON object is not possible in this sense, if you want it to show up in the $_POST variable automatically. There are a few options from here:

选项一:直接使用帖子正文而不是使用$_POST.使用fopen("php://input")并使用PHP JSON解析器进行解析:

Option one: Rather than using $_POST, use the post body directly. Use fopen("php://input") and parse that using a PHP JSON parser:

$input = file_get_contents("php://input");
$obj = json_decode($input,true);
$restIDs = $obj['restIDs'];

如果您走了这条路线,则无需创建带有名为restIDs的字段的对象.相反,您可以简单地序列化数组并将$obj用作$restIDs

If you went this route you need not create an object with a field named restIDs though. You can instead simply serialize the array and use $obj as $restIDs

第二种方法,假设您在restID中的对象只是字符串,而不是将数据作为JSON对象传递,而是将其格式设置为供PHP使用:

Option two, assuming your objects in restIDs are simply strings, instead of passing the data in as a JSON object, format the body as intended for PHP use:

NSMutableString *bodyStr = [NSMutableString string];
for (NSString *restID in restaurants) {
    [bodyStr appendFormat:@"restIDs[]=%@&",[restID stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
}
NSData *body = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody: body];

现在,您应该能够像预期的那样使用$_POST['restIDs']访问它.

Now you should be able to access it using $_POST['restIDs'] as you would expect.

这篇关于使用iOS上的POST将数组传递给PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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