2

If I have a php file on my site, how could I pass an NSString to a POST variable (with objective-c) so I can echo it onto the page? I am new to php, so sorry if this is obvious.

Using ASIFormDataRequest:

NSURL *myURl = [[NSURL alloc] initWithString:@"http://www.mysite/something.php"];

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:myURl];

[request setDelegate:self];
[request setUsername:@"username"];
[request setPassword:@"password"];
[request setPostValue:@"This is a string" forKey:@"phpVar"];

[request startAsynchronous];

Delegate method calls:

-(void)requestFailed:(ASIHTTPRequest *)request
{
    NSError *error = [request error];
    NSLog(@"Failed %@ with code %d and with userInfo %@",[error domain],[error code],[error userInfo]);
}

-(void)requestFinished:(ASIHTTPRequest *)request
{
    NSLog(@"Finished : %@",[request responseString]);

}

There are a couple of problems here:

  1. Even if I enter the incorrect credentials, requestFinished only gets called.

Edit -- Just fixed (1), created a password protected directory and stuck the php file in there.

  1. "This is a string" isn't getting passed to phpVar in the php file and won't display on the page

Here is what I wrote in php:

<?php

$blah = $_POST['phpVar'];

echo $blah;

?> 

Thanks for your help.

Taylor Gerring
  • 1,825
  • 1
  • 12
  • 17
Imirak
  • 1,323
  • 11
  • 21
  • 1
    Debug it one step at a time. Start by returning a dummy string from PHP, like "Hello world". – Seva Alekseyev Jul 16 '12 at 19:06
  • Of course I've already done that. Echoing any string onto the page works fine. – Imirak Jul 16 '12 at 19:07
  • Pro tip: NSString is just a wrapper for string. Just encode the string as ASCII bytes (or whatever), then pass those bytes in the post variable. – Cole Tobin Jul 16 '12 at 19:08
  • How big is your php-script on the server? Add it to your question, if it's too big, put it on pastebin.com or elsewhere. – ott-- Jul 16 '12 at 19:09
  • ott I posted the php. And @cole, whoops I thought setPostValue:forKey: only took NSStrings, but I guess I was mistaken. In either case, I encoded the string (NSUTF8) and still no luck. – Imirak Jul 16 '12 at 19:15
  • And I forgot something: what is in responseString? What's in the apache logs on the server (both access and error log)? – ott-- Jul 16 '12 at 19:28
  • In response string is "This is a string" – Imirak Jul 16 '12 at 19:49
  • That's the correct answer. If you want your credentials to be checked, you should enable an auth scheme like basic auth in `.htaccess`. – ott-- Jul 16 '12 at 20:18
  • Hmm ok. But then why isn't "This is a sting" displayed on the page of the url? Also, I get a status code of 0 when I log the status code using: `statusCode = request.responseStatusCode` – Imirak Jul 16 '12 at 20:23
  • @ott-- do you know what's going on? – Imirak Jul 17 '12 at 01:34
  • It looks as if you don't have the real understanding of what php does. When you call something.php, your request is processed, the result is normally some generated html-code, which you will receive in responseString. You can feed this string into an UIWebView, which will display the resulting page. – ott-- Jul 17 '12 at 07:37
  • @ott-- That may be so, but I think you may be misunderstanding my question. I fixed my credential issue by password protecting the directory which contained the php file. The passing of the string into phpVar is successful. How would I echo it onto the page though? Not onto a UIWebView, but onto `www.mysite.com/myprotecteddirectory/myfile.php` – Imirak Jul 17 '12 at 18:10
  • 1
    For that purpose myfile.php needs to modify itself (or write the value of `$_POST['phpVar']` to a config file which is read when the next one calls the page). And at the end of the script you need to output some html like `echo "..."; echo $_POST['phpVar']; echo "";`. – ott-- Jul 18 '12 at 07:40

1 Answers1

0

There's potentially a few different places that this process could break down.

  1. Since you're attempting to set a literal in your instance of ASIFormDataRequest, we can probably safely assume it actually contains that value.
  2. The next item to validate is that the POST is actually hitting the PHP page (but not necessarily passing the correct values). There are a few ways to accomplish this, but the easiest may be to view the access log of the web server. How to accomplish this will vary depending on the server (i.e. Apache, IIS, etc.).
  3. If the script is indeed hitting the page, the last step should be validating what (if any) request parameters are bing passed. Instead of dumping out the singular value form $_POST, I would recommend you take a broader look at all the request values by echoing them out with a function call like so:

.

<?php
var_dump($_REQUEST);
?>

This should verbosely print out everything in $_GET, $_POST, and $_COOKIE, which can be handy just in case the value isn't getting sent through a POST, as you'd expect. If the value is present, it should be trivial to access it or modify the way in which the request was sent so that it is delivered as expected.

Taylor Gerring
  • 1,825
  • 1
  • 12
  • 17