0

I've created an API for use on my website.

The API I made strips everything using mysql_real_escape_string then puts it into the database.

But the problem I'm having is the URL that my php scripts are using to access the API is cut short sometimes...

Which I have narrowed down to one of the parameters...

When its Ford Mondeo 22' the URL that is passed to simplexml_load_file is


http://mydomain.com/api/create.xml?api_number=brho15p6z1dhqwf5tsff&env=live&number=AJ20023232&title=Ford Mondeo 22'&image=http://mydomain.com/wp-content/uploads/2012/10/914955-150x150.jpg


but the API reports back the URL accessed as


http://mydomain.com/api/create.xml?api_number=brho15p6z1dhqwf5tsff&env=live&number=AJ20023232&title=Ford


If I remove the single quote then everything works fine, any idea how to correct this I suspect there's something I've overlooked when passing variables in the URL

1 Answer 1

2

It is the spaces in the "Ford Mondeo 22'" value that is causing the problem. You cannot have a spaces in the URL. You need to use escape characters. The encoded version of the parameter should be

Ford%20Mondeo%2022'

%20 is the escape character for space

I.e. the whole URL should read as follows:

http://mydomain.com/api/create.xml?api_number=brho15p6z1dhqwf5tsff&env=live&number=AJ20023232&title=Ford%20Mondeo%2022'&image=http://mydomain.com/wp-content/uploads/2012/10/914955-150x150.jpg

EDIT: Your comment indicates that you use PHP. In PHP, you can use urlencode($foo) and urldecode($foo) to switch between the normal string and the encoding string.

1
  • Thanks is there a way to do this in PHP? Preferably to the entire url. Oct 16, 2012 at 12:53

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.