0

I have a .php file that takes some information from the parameters in its URL. This was working fine until I realized that each time I called the URL, there would be a different amount of variables.

Example:

www.myurl.com/webserivce?var1=20&var2=30

www.myurl.com/webserivce?var1=20&var2=30&var3=40

If I have 3 variables in my .php file, and only send the URL with 2, it crashes.

One potential work around is that I have 3 variables, and only send 1, I could fill the other 2 with 'NULL' or something like that: www.myurl.com/webserivce?var1=20&var2=NULL&var3=NULL

I was just wondering if there was a better way.

BTW I am sending this URL request from an iOS app.

2
  • 1
    You could always write your PHP to check to see if the variables are set, and to use an empty value if they're not. Commented Aug 9, 2012 at 16:53
  • in php you can declare predefined variables values in your methods: function example($param1, $param2 = "", $param3 = null) {} Commented Aug 9, 2012 at 16:56

4 Answers 4

1

Why not do a POST instead and send along a structured data format?

You could construct a JSON object easily in Obj-C, send that, and then decode it in PHP. It will be much more flexible.

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

3 Comments

would this allow me to send varying amounts of parameters ?
Yes. You would construct an array in Obj-C, for example, which would get converted to a JSON representation of an array, and then decode it back to an array in PHP.
after looking into JSON, this is what I will use
0

This is your friend. You're probably not checking to see if the variables are set and trying to do things with them hence resulting in a crash due to an access on a NULL variable.

2 Comments

so in .php, it is okay to send a url with insufficient amount of arguments, as long as I set them in my .php script ?
@HelloWorld As long as you don't try to access those arguments in your scripts.
0

you can set the variables to null in the php script:

if(!isset($_GET['var1'])) $_GET['var1']="NULL";
if(!isset($_GET['var2'])) $_GET['var2']="NULL";
if(!isset($_GET['var3'])) $_GET['var3']="NULL";

Therefore, sending www.myurl.com/webserivce?var1=20&var2=30 would be the same as www.myurl.com/webserivce?var1=20&var2=30&var3=NULL

Comments

0

I think the best way it is pre-initialization all variables in your web-service

Comments

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.