1

I have read up quite a bit about the request method now, and i found a example that supplies this php example :

<?php
  // Read request parameters
  $firstName= $_REQUEST["firstName"];
  $lastName = $_REQUEST["lastName"];// Store values in an array
  $returnValue = array(“firstName”=>$firstName, “lastName”=>$lastName);
  // Send back request in JSON format
echo json_encode($returnValue); 
?>

i have written a IOS app in swift that sends data to this php, i will then use the website and write the data received to a textfile on the server, is the $_Request method the correct method ?

IOS part ( not complete code ) that sends data to the website :

let request = NSMutableURLRequest(URL: NSURL(string: "http://localhost/test.php")!)
    request.HTTPMethod = "POST"
    let postString = "firstName=Neil&lastName=Johnson"

Note : this code does not seam to work ( Undefined index at line 4, 5 and 6 but im not sure why as i cant use a isset method here as some threads have stated to remove the error)

Thank you in regards

4
  • Try $_POST instead of $_REQUEST Commented Nov 8, 2015 at 18:50
  • The $_REQUEST superglobal contains the contents of $_GET and $_POST so it should work anyway Commented Nov 8, 2015 at 18:51
  • Check your quotes. And install a proper text editor/ide with syntax highlighting support Commented Nov 8, 2015 at 18:51
  • You should use $_POST and use standard double quotes Commented Nov 8, 2015 at 18:51

2 Answers 2

2

Firstly, $_REQUEST is not a "method", it's a "superglobal variable". Methods are called using parentheses(). Arrays (such as this) are accessed via square brackets[].

"Undefined index" means you're trying to access an array element that doesn't exist, which suggests to me that you aren't POSTing the firstName and/or lastName correctly to PHP.

Generally you shouldn't be composing your POST message manually. Isn't there something in iOS/Swift to form-encode your variables? If not, did you set the Content-Type to application/x-www-form-urlencoded? That could be it as well.

Lastly, your quotes on this line

$returnValue = array(“firstName”=>$firstName, “lastName”=>$lastName);

Are weird. You need to use the standard " found on your keyboard. If you're copy-pasting something from a blog, WordPress likely mangled them for you.

Sign up to request clarification or add additional context in comments.

2 Comments

yeah sorry only saw that quotes now, weird. my data is successfully being sent to the website now so thanks for the help ! cheers
so i got it working pretty good with using the $_request variable ... i am now able to send values to the website, can this also be used to retrieve data from the website to my device ? my next step is to be able to retrieve values as well ! thanx in advance
1

This one confuses quite a few people so I'm going to attempt to explain this as objectively as possible.

PHP has a few super global variables which are automatically populated based on certain conditions.

When to use $_POST

You should use the $_POST superglobal to access your data when you expect the HTTP request method to the resource to be POST and nothing else. The POST request verb is typically used for non-idempotent requests (like checking out a shopping cart or adding a post to a web forum).

Note that $_POST only populates when the Content-type header is www-form-urlencoded or multi-part encoded. Otherwise you will have to check php://input since PHP probably won't decode it into $_POST. PHP also mangles the names a bit (e.g. it replaces spaces and dots with underscores). See Handling External Variables for more details.

When to use $_GET

Adversely, you should use $_GET when expect the input to be a part of the query string or in forms where you've explicitly set the request method to GET. Notice this doesn't necessarily mean you expect the HTTP request verb to be GET every time.

When to use $_REQUEST

You use $_REQUEST when you actually don't care whether the HTTP request verb was POST or GET, because $_REQUEST combines both into one array. In this case it's important to recognize that if the same name is populated both in POST and GET that there is a specific order in which they will be overwritten.


It's always better to be explicit as in a RESTful approach you would rarely rely on $_REQUEST. In your case you seem to be explicitly sending a POST request so I would stick with $_POST in this case. But you very well may send POST request and still expect the input in the query string, in which case $_GET is appropriate. I've very rarely seen a real need for $_REQUEST in the majority of cases.

4 Comments

How does this answer the question?
It explains when it is appropriate to get the input from which super global without making assumptions about the OP's implementation. For example, it explains that $_POST expects a url-encoded Content-type. It also explains variable_order configuration when dealing with $_REQUEST. It also explains how external names can be mangled in $_POST/$_GET/$_REQUEST keys. In what way do you feel that this does not answer the question? I'm happy to modify the answer.
The additional information you have provided is useful, but his problem was an error of Undefined Index. mpen above has pointed this out to the OP. Perhpas modify your answer to point out that mpen has solved the issue and you are providing further information on the $_REQUEST variable.
I was in the process of typing up this answer before I saw any other answers submitted. It was unclear what caused the undefined index error so I included relevant information that would help address that. I don't see any reason why this does not help address the question, however.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.