PDA

View Full Version : Sending Text to php!




nunikasi
Jun 26, 2008, 08:53 AM
Hi. Im trying to make a little application that sends text to a website.

I will have a window.
In this window ill make a text field.
When i type "123" in that field and click send, i want it to send it to a php file so i can instantly accsess it on my webserver.

Can someone please tell me how to do this?

Thank you :)



pjrobertson
Jun 26, 2008, 09:01 AM
Not sure on the Cocoa side of things (still learning) but you'll need to set up a PHP form with a GET method in it, then get your program to send the data like

http://example.com/form.php?data=123

Not the most secure way, but works most of the time.

http://www.w3schools.com/PHP/php_get.asp

Maybe you could use webkit or some command line (curl? wget?) to 'ping' the URL you want.

Enuratique
Jun 26, 2008, 10:03 AM
Not sure on the Cocoa side of things (still learning) but you'll need to set up a PHP form with a GET method in it, then get your program to send the data like

http://example.com/form.php?data=123

Not the most secure way, but works most of the time.

http://www.w3schools.com/PHP/php_get.asp

Maybe you could use webkit or some command line (curl? wget?) to 'ping' the URL you want.

NSMutableRequest's setHTTPMethod and setHTTPBody methods....

nunikasi
Jun 26, 2008, 10:29 AM
Please make a tutorial or something?

Thanks

Enuratique
Jun 26, 2008, 07:19 PM
Please make a tutorial or something?

Thanks

This is going to be very basic as I don't really have time to make a full tutorial but this may help you get on your way.

First create a PHP form that accepts POST data [HTTP command to upload content to a web server]

Then do something along the lines of:


NSURL *url = [NSURL URLWithString: @"http://www.mysite.com/formThatSavesDataSomehow.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: url];

[request setHTTPMethod: @"POST"];
[request setHTTPBody: [[[NSString alloc] initWithString: @"text=yourValueGoesHere"] dataUsingEncoding: NSASCIIStringEncoding]];

NSURLConnection* theConnection = [NSURLConnection connectionWithRequest: request delegate: self];

// Somehow invoke this connection? You'll have to figure this one out


Then your PHP form can simply reference the POST variable $_POST["text"]. You can display that value back to the user, or you can save it to a database... I don't know what you're trying to accomplish here.

[EDIT:] I just realized, if all you're trying to do is pass a value off to a PHP form, you could just build the url like so:


NSString *uri = [NSString stringWithFormat: @"http://www.mysite.com/formThatSavesDataSomehow.php?text=%@", textField.text];
NSURL *url = [NSURL URLWithString: uri];
NSURLRequest *request = [NSURLRequest requestWithURL: url];
NSURLConnection* theConnection = [NSURLConnection connectionWithRequest: request delegate: self];


Thereby saving you from having to deal with all that POST crap. Then in the PHP you pull it out of the request variable $_REQUEST["text"];

HTH,

Enuratique

sord
Jun 26, 2008, 07:38 PM
Make sure you encode the values from your textbox (for example & would need to be %26)

See http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
(I'm sure Cocoa has a builtin way of doing this, but I'm no Cocoa expert)

nunikasi
Jun 28, 2008, 09:17 AM
Thank you for the help.

Here is my project folder, This (http://www.dump.no/files/a45f5fbce230/Eksempel.zip)

I write something in the field, press enter and it shows bellow the field.
I want it to show in the browser!

Can someone help me with this?
Just send me the new control.h/m you got from the link above.

Thanks! :) :D

lee1210
Jun 28, 2008, 10:27 AM
No one is going to do this work for you. You got examples with code, when references to the classes involved would have been sufficient.

I would recommend sticking with POST if you don't know the size of the entry. There are various limitations on the length of GET requests across different servers and browsers, but I don't know what it would be for this particular Obj-C class. POST is always safer as you can send much larger parameters, and technically the intent is that if you are changing state things should always be POST, and GET is only used for "reading" things with parameters (i.e. pulling up an auction listing based on id). This isn't really abided by, but it's still a decent rule of thumb.

It's also not clear what your intention is. Do you want to just send something to a web server for storage, etc.? Or do you just want to open the URL in a browser control/window? Interface builder lets you embed a browser control in your own app that could be used. If you want to open a separate browser, look at NSWorkspace's openURL or openURLs:withAppBundleIdentifier:options:additionalEventParamDescriptor:launchIdentifiers:.

-Lee