Sending Text to php!

Discussion in 'Mac Programming' started by nunikasi, Jun 26, 2008.

  1. nunikasi macrumors newbie

    Jun 23, 2008
    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 :)
  2. pjrobertson macrumors 6502a

    Nov 14, 2007
    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

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

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

    Apr 28, 2008
    NSMutableRequest's setHTTPMethod and setHTTPBody methods....
  4. nunikasi thread starter macrumors newbie

    Jun 23, 2008
  5. Enuratique macrumors 6502

    Apr 28, 2008
    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: @""];
    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: @"", 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"];


  6. sord macrumors 6502

    Jun 16, 2004
  7. nunikasi thread starter macrumors newbie

    Jun 23, 2008
    Thank you for the help.

    Here is my project folder, This

    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
  8. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    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


Share This Page