How to POST to PHP (and read the response)?

Apprentice
Posts: 16
Joined: 2008.10
Post: #1
I've tried Google for over a week now and still can't find a simple example that shows how to post to a PHP website (how to send variables to it.) I found out how to in C# in about 10 seconds (and it worked.)

So, my question is, if I have a PHP site that accepts variables, how do I send those to the site from Objective-C? Better yet would be an example that shows me how to transmit and receive XML from a PHP site.

For example, "www.mysite.foo/turn.php" and I want to send "game=mygame&username=myusername&password=mypassword"

In response I would like to see "Turn Received" or something of the like.

There are many apps on the store that use websites to store high-scores and such. Are they using a library?
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #2
If you pass it through the URL like that, it's actually using GET rather than POST. But anyway, to send the data for Juggler's high scores, I create an NSURLConnection and simply open it up to the url I want like "www.mysite.com/turn.php?hi=5&d=12". Then my server spits out XML for the response. So my PHP script looks a bit like this:

Code:
<?php
// handle the data, insert scores, do whatever

header('Content-type: text/xml');
header('Pragma: public');        
header('Cache-control: private');
header('Expires: -1');
echo '<?xml version="1.0" encoding="utf-8"?>';
echo '<highscores>';
// write out the high scores
echo '</highscores>';
?>

So when the NSURLConnection is doing it's thing, you just keep saving the data down to an NSMutableData. When the NSURLConnection is done, I then shell off that data to an NSXMLParser creating the XML from the data.
Quote this message in a reply
Moderator
Posts: 613
Joined: 2004.09
Post: #3
For the Obj-C Interaction


NSString *string = [NSString stringWithContentsOfURL: @"www.php.com/somescript?$name=x"];

for your example....

//Typed in safari check commands
NSString *urlString;
urlString = [NSString stringWithFormat: @"www.mysite.foo/turn.php$game=%@&username=%@&password=%@", gamename, username, password];

NSString *resultString = [NSString stringWithContentsOfURL: [NSURL urlWithString: urlString]];

NSLog(@"Got back %@ from PHP Script", resultString);

Kyle Richter
DragonForged.com
Twitter: @kylerichter
Quote this message in a reply
Apprentice
Posts: 16
Joined: 2008.10
Post: #4
Nick Wrote:If you pass it through the URL like that, it's actually using GET rather than POST. But anyway, to send the data for Juggler's high scores, I create an NSURLConnection and simply open it up to the url I want like "www.mysite.com/turn.php?hi=5&d=12". Then my server spits out XML for the response. So my PHP script looks a bit like this:

Code:
<?php
// handle the data, insert scores, do whatever

header('Content-type: text/xml');
header('Pragma: public');        
header('Cache-control: private');
header('Expires: -1');
echo '<?xml version="1.0" encoding="utf-8"?>';
echo '<highscores>';
// write out the high scores
echo '</highscores>';
?>

So when the NSURLConnection is doing it's thing, you just keep saving the data down to an NSMutableData. When the NSURLConnection is done, I then shell off that data to an NSXMLParser creating the XML from the data.
Hey Nick! Glad to see you here (seen you around at the XNA forums). Smile

One question, can you share your NSURLConnection code? I've tried the reference guide and tried sample code found out "there" but it's not exactly workable (everything crashes, hangs, or doesn't compile.) I do have the PHP page working from C# so I believe it is "correct".

Kodex, thanks for the code but for some reason I get a warning on "NSString *resultString = [NSString stringWithContentsOfURL: [NSURL urlWithString: urlString]];" as the iPhone code I am using does not recognize the method. Also, it crashes out to the debugger when run or if I comment out that line. I did change it to my server by the way.

Thanks for all the help so far!
LL
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #5
Hey Lostlogic. I like to get around the interwebs a bit. Rasp

Sure. Here's my entire high score system code: http://pasteall.org/4009/objc. I put both the .h and .m files into one, but there's a big //******** row in there separating them. I'm not going to say it's the most secure system (I'm still getting used to working in a non-GC environment), but it seems to work quite well and I haven't gotten any reports of crashes or errors during submission or retrieval of scores.

Let me know if you need any more of it explained.
Quote this message in a reply
Apprentice
Posts: 16
Joined: 2008.10
Post: #6
Nick Wrote:Hey Lostlogic. I like to get around the interwebs a bit. Rasp

Sure. Here's my entire high score system code: http://pasteall.org/4009/objc. I put both the .h and .m files into one, but there's a big //******** row in there separating them. I'm not going to say it's the most secure system (I'm still getting used to working in a non-GC environment), but it seems to work quite well and I haven't gotten any reports of crashes or errors during submission or retrieval of scores.

Let me know if you need any more of it explained.

Wow, thanks a ton Nick!

@kodex, I got your code to work, it looks like I just needed to put URLWithString in caps (and not just blindly copy your code.) Thanks for supplying it. Smile
Quote this message in a reply
Apprentice
Posts: 16
Joined: 2008.10
Post: #7
Nick, one thing, I see you helped a ton with your obj-c code, can you post your php as well with the XML so I can utilize it? I'll be sure to share the updates I make to ensure it's compatible with my state-based game.

Thanks again,
LL
Quote this message in a reply
Moderator
Posts: 613
Joined: 2004.09
Post: #8
Lostlogic Wrote:@kodex, I got your code to work, it looks like I just needed to put URLWithString in caps (and not just blindly copy your code.) Thanks for supplying it. Smile

Yea sorry about that, I typed it in Safari, didnt catch that one, I am huge on tab complete in Xcode so I dont hit those capitalization issues.

Kyle Richter
DragonForged.com
Twitter: @kylerichter
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #9
Here are my two PHP files I'm using: http://pasteall.org/4010/php. It's really quite simple stuff. Just do some data management with a MySQL server, set up the headers for the XML, and just echo the XML document.
Quote this message in a reply
Apprentice
Posts: 16
Joined: 2008.10
Post: #10
Nick Wrote:Here are my two PHP files I'm using: http://pasteall.org/4010/php. It's really quite simple stuff. Just do some data management with a MySQL server, set up the headers for the XML, and just echo the XML document.

Thanks again!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Multi touch response time captainfreedom 12 7,311 May 9, 2009 12:35 PM
Last Post: Frank C.
  glGetTexImage for iPhone or how read pixels from texture? frankinshtein 0 4,032 Sep 29, 2008 03:17 AM
Last Post: frankinshtein
  read wifi/hotspots-names - how? gamela1n 3 3,103 Jul 28, 2008 10:36 AM
Last Post: AnotherJake