Using POST to .php

Viewed 844

EDIT: I have edited the OP with all the suggestions I got from people, so this is the latest (and its still not working).


I have the following code, which is supposed to POST some data to a .php file I have (and then to a database but that is beyond the scope of this question).

To my method, I pass an array that contains some strings.

-(void)JSON:(NSArray *)arrayData {

    //parse out the json data
    NSError *error;

    //convert array object to data
    NSData* JSONData = [NSJSONSerialization dataWithJSONObject:arrayData 
                                                       options:NSJSONWritingPrettyPrinted 
                                                         error:&error];

    NSString *JSONString = [[NSString alloc] initWithData:JSONData 
                                                 encoding:NSUTF8StringEncoding];


    NSString *URLString = [NSString stringWithFormat:@"websitename"];

    NSURL *URL = [NSURL URLWithString:URLString];

    NSLog(@"URL: %@", URL);


    NSMutableURLRequest *URLRequest = [NSMutableURLRequest requestWithURL:URL 
                                                              cachePolicy:NSURLRequestUseProtocolCachePolicy 
                                                          timeoutInterval:60.0];

    NSData *requestData = [NSData dataWithBytes:[JSONString UTF8String] length:[JSONString length]];

    NSString *requestDataString = [[NSString alloc] initWithData:requestData 
                                                        encoding:NSUTF8StringEncoding];
    NSLog(@"requestData: %@", requestDataString);

    [URLRequest setHTTPMethod:@"POST"];
    NSLog(@"method: %@", URLRequest.HTTPMethod);
    [URLRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [URLRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [URLRequest setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
    [URLRequest setHTTPBody: requestData];

    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:URLRequest delegate:self];

    if (connection) {
        NSMutableData *receivedData = [[NSMutableData data] retain];
        NSString *receivedDataString = [[NSString alloc] initWithData:receivedData 
                                                             encoding:NSUTF8StringEncoding];
        NSLog(@"receivedData: %@", receivedDataString);
    }

    //potential response
    NSData *response = [NSURLConnection sendSynchronousRequest:URLRequest returningResponse:nil error:nil];

    //convert response so that it can be LOG'd
    NSString *returnString = [[NSString alloc] initWithData:response encoding:NSASCIIStringEncoding];

    NSLog(@"RETURN STRING: %@", returnString);
}

NOTE: I have the actual URL of my website in the code, so that anyone can test if they want to.


Then there is the .php side of things, where all I am trying to do is look for any input at all (but I am getting nothing).

<html>
        <body>
        <?php

        echo "Connection from iOS app to MySQL database<BR>";

        echo '<pre>'.print_r($GLOBALS,true).'</pre>';



        ?>
        </body>
</html>

All I am really trying to do right now is confirm the data is getting through, by echoing it to the page, and then reading the page from my app (so far the data hasn't been showing up).


The output of the .php:

 <html>
        <body>
        Connection from iOS app to MySQL database<BR>'Array
(
)
'<pre>Array
(
    [_GET] => Array
        (
        )

    [_POST] => Array
        (
        )

    [_COOKIE] => Array
        (
        )

    [_FILES] => Array
        (
        )

    [GLOBALS] => Array
 *RECURSION*
)
</pre>        </body>
</html>
3 Answers
Related