Writing JSON data to a plain text file

As always, I searched the forums and went a little crazy, not being able to understand what I was doing wrong. Therefore, I appeal to the great minds who visit this site, hoping to find the answer. I am creating an application that will communicate with the database, and at the same time I am trying to learn how to use JSON to retrieve and send data to the database via iPhone, using various examples found on the Internet. I managed to get data from the Internet using JSON and show it as a table, however, when I try to get POST data, nothing happens. I have a simple php script that should write the received data to a text file (see below).

<?php
//header('Content-type: application/x-json');

$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");

$stringData = var_dump($_POST);
fwrite($fh, $stringData);

$stringData = "=== JSON Decoded ===";  
fwrite($fh, $stringData);

$stringData = $_POST["tmp"];
fwrite($fh, json_decode($stringData));

$stringData = "=== JSON Decoded ===";
fwrite($fh, $stringData);

fclose($fh);
?>

, script, , . , . , , .

=== JSON Decoded ====== JSON Decoded ===

POST XCode.

-(IBAction)poststuff:sender{

    NSString *stuffToPost = [[NSString alloc] initWithFormat:@"Work, damn you!"];

    NSURL *jsonURL = [NSURL URLWithString:@"http://localhost:8888/iWish/json_post.php"];

    NSData *postData = [stuffToPost dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    NSLog(@"Stuff I want to POST:%@", stuffToPost);
    NSLog(@"Data I want to POST:%@", postData);

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:jsonURL];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    NSError *error;
    NSURLResponse *response;

    NSData *serverReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    NSString *data = [[NSString alloc] initWithData:serverReply encoding:NSUTF8StringEncoding];
    NSLog(@"Raw Data:", data);
}

, :

2010-10-04 14:10:16.666 iWish[38743:207] Stuff I want to POST:Work, damn you!
2010-10-04 14:10:16.668 iWish[38743:207] Data I want to POST:<576f726b 2c206461 6d6e2079 6f7521>
2010-10-04 14:10:16.673 iWish[38743:207] serverReply:

, , , -, - . , - , .

. !

+5
1

fwrite , var_dump . ..., json_decode , JSON.

, , :

$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = json_encode($_POST);
fwrite($fh, $stringData);
fclose($fh);
+3

All Articles