title: How to return a value from pHp server script to NSMutableData (Objective C, iOS)
body: <p>Ok, I'm having trouble finding an answer to this that isn't just use JSON or ASIHTTPRequest.  But my question is a little more specific, I think.  I only program as a hobby, so I'm no expert.  With that said, my abstract understanding of this could be completely off base, so the question might not be answerable.</p>
  
  <p>Basically, what I would like to do is send an HTTP request to a pHp script on a web server an have the web server return a value, lets say a simple integer value or bool value (think a login script that returns a yes/no).  I'm not worried about security at this point as I'm trying to ease my way into understanding this.</p>
  
  <p>From what I gather, I'd use something to the effect of </p>
  
  <pre><code>// Create the request.
  NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]
                      cachePolicy:NSURLRequestUseProtocolCachePolicy
                  timeoutInterval:60.0];
  // create the connection with the request
  // and start loading the data
  NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
  if (theConnection) {
     // Create the NSMutableData to hold the received data.
     // receivedData is an instance variable declared elsewhere.
  receivedData = [[NSMutableData data] retain];
  } else {
  // Inform the user that the connection failed.
  }  
  </code></pre>
  
  <p>This is from the Apple documentation, and I generally understand this.  So lets say it sends information through the URL to a pHp file which then:</p>
  
  <pre><code>//pHp file gets information
  //pHp file uses that information to check against a mySQL database
  //pHp now has the variable $result with a value of 1 in it
  </code></pre>
  
  <p>Also, assume I can do all of that fine.  My question becomes, how do I return a value from pHp back to the iOS 'NSMutableData' variable.  Do I 'echo' it out? For example:</p>
  
  <pre><code>echo "$result";
  </code></pre>
  
  <p>And then thats the information put into the NSMutableData (the variable $result)?  I know if I were doing it strictly through pHp/HTML etc., I might use something like a session variable to allow further pages to access the variable.</p>
  
  <p>Or is there some other pHp function that returns the data to NSMutabalData that I'm unaware of?  Basically this is the link between the two that I'm having trouble with, the returning of data from the pHp script - I don't really understand what it is that NSMutableData is 'getting'.</p>
  
  <p>I've tried to be as detailed as possible, so sorry for the long question.  Please just assume this is all I'm trying to do, return a value.  Security isn't important (I'm trying to do this at the most simple level to understand it then build on it).  I'm trying to avoid any vague answers such as 'write a web service' or 'use <strong>_</strong>' to do it, and I'm sure there are lots of better ways to do it, but I'm not rushing out a program - I'm just trying to learn this for a better understanding of NSURLConnection, pHp, NSMUtableData and how al the cogs fit together.  If what I'm asking is technically impossible and doesn't make sense, I appreciate that answer too.  I've tried searching and haven't found a satisfactory answer for this single piece of the puzzle.</p>
  
  <p>Thanks in advance.</p>
  
  <p>EDIT:</p>
  
  <p>I've tried </p>
  
  <pre><code>...
  NSMutableData *receivedData = [[NSMutableData alloc] init];
  receivedData = [[NSMutableData data] retain];
  NSString *receivedDataString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
  label.text = receivedDataString; 
  ...
  </code></pre>
  
  <p>Which calls a pHp file on the server that is basically:</p>
  
  <pre><code>&lt;?pHp
  $result = "TRUE";
  var_dump($result);
  ?&gt;
  </code></pre>
  
  <p>Which doesn't work.  I think my problem is on the pHp side, is there some special command that returns a value to the NSMutableData object?  (Just some extra info to maybe clarify my question more).</p>
  
  <p>EDIT II:</p>
  
  <p>I can't seem to respond directly, probably because I've only ever used the site anonymously before and I have a brand new account :)  Thanks for your answer Charles, I've tried to use echo and var_dump, neither seem to send the data back (my NSMutableData object when converted to an NSString seems to be empty - unless my objective C is wrong, but I've parsed it down to pretty much the bare minimum so I don't believe that's the case).</p>