如何检索从iOS发送的php中的数组 [英] How to retrieve array in php sent from iOS

查看:38
本文介绍了如何检索从iOS发送的php中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我将 NSmutable Array 发送到 PHP webservice 的 iOS 代码:

Following is my iOS code that sends NSmutable Array to PHP webservice:

 // Getting Server Address
            AppDelegate *appDelegate =
            [[UIApplication sharedApplication] delegate];

            NSString *serverAddress = [appDelegate getServerAddress];

            serverAddress = [serverAddress stringByAppendingString:@"ABC.php"];


            NSLog(@"Server Address: %@",serverAddress);

            NSData *post = [NSJSONSerialization dataWithJSONObject:UsersArray options:NSJSONWritingPrettyPrinted error:nil];
            NSString *postLength = [NSString stringWithFormat:@"%d", [post length]];
            NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:serverAddress]];

            [request setHTTPMethod:@"POST"];
            [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
            [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
            [request setHTTPBody:post];
            [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
            [request setTimeoutInterval:30];
            NSOperationQueue *queue = [[NSOperationQueue alloc] init];
            [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse* theResponse, NSData* theData, NSError* error){
                //Do whatever with return data

                NSString *result = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];
                NSLog(@"Result : %@",result);
            }];

我想在 PHP 中检索该数组.我该怎么做?

I want to retrive that array in PHP. How can I do that?

这是我尝试过但返回 null 的 php:

Here is the php which I tried but returns null:

$handle = fopen('php://input','r');
$jsonInput = fgets($handle);
// Decoding JSON into an Array
$decoded = json_decode($jsonInput,true);

echo json_encode($decoded);

推荐答案

我使用了一个稍微不同的 Content-Type(应该无关紧要):

I use a slightly different Content-Type (which shouldn't matter):

[request addValue:@"text/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

我也使用稍微不同的 PHP:

And I use a slightly different PHP, too:

<?php

$handle = fopen("php://input", "rb");
$http_raw_post_data = '';
while (!feof($handle)) {
    $http_raw_post_data .= fread($handle, 8192);
}
fclose($handle); 

$json_data = json_decode($http_raw_post_data, true);

echo json_encode($json_data);

?>

如果你得到一个空白的响应,我敢打赌你有一些 PHP 错误.我希望您查看服务器的错误日志,或者临时更改 php.ini 中的 display_errors 设置,如下所示:

If you're getting a blank response, I'd wager you have some PHP error. I'd you check out your server's error log, or temporarily changing the display_errors setting in your php.ini as follows:

display_errors = On

这篇关于如何检索从iOS发送的php中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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