Search code examples
phpafnetworking-2json-deserialization

AFNetworking 2.0 to PHP with POST request - how to get an Array


I am sending data from AFNetworking 2.0 to PHP. When I get the content with $_POST and display it with var_dump like this:

var_dump($_POST['PARAMS']);

I can see the following shown:

string(xx) "Array (     [age] => xx        [email] => xxxxxx     [facebookid] => xxxxxxx     [firstname] => xxxxxx      [type] => x )"

my question is how do I convert this into a real array?

I tried to use json_decode, but it always returns null. Is there a simple way to convert this into an array?

for information this is how I send my request in my objective C

        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

        manager.responseSerializer = [AFJSONResponseSerializer serializer];
        manager.requestSerializer = [AFJSONRequestSerializer serializer];

        [manager POST:URL parameters:parameters

Solution

  • You can use AFHTTPSessionManager POST method,

    (NSURLSessionDataTask *)POST:(NSString *)URLString parameters:(id)parameters constructingBodyWithBlock:(void (^)(id<AFMultipartFormData>))block success:(void (^)(NSURLSessionDataTask *, id))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure;
    

    pass parameters as NSMutableDictionary to server side (PHP), then you can access like

    $_POST['age'], $_POST['email'], $_POST['facebook'], $_POST['firstname'], $_POST['type']