0

I have the following json encoded object:

{"username":"my_username","email":"my_email","password":"12345678","confirm_password":"12345678"}

and I want to convert this into url string so I can use it with my REST API function for example like this one:

search?search=asdadd%2C+United+Kingdom&type=tutor

I have found many functions in javascript to do this but I haven't been able to find anything in PHP. What is the function in PHP to do this?

3
  • 3
    decode -> http build query -> done Commented Mar 10, 2016 at 23:55
  • 1
    How is that json related to the url? Anyway, take a look at json_decode Commented Mar 10, 2016 at 23:57
  • Incidentally, are you sure you want to send passwords in query strings? Commented Mar 11, 2016 at 0:07

1 Answer 1

2

The following query string:

?username=my_username&email=my_email&password=12345678&confirm_password=12345678

.. will turn into:

{"username":"my_username","email":"my_email","password":"12345678","confirm_password":"12345678"}

If you use json_enconde.

To reverse the process, you need to use json_decode as well as http_build_query.

First, turn the JSON into an associative array with json_decode:

$json = '{"username":"my_username","email":"my_email","password":"12345678","confirm_password":"12345678"}';

$associativeArray = json_decode($json, true);

Now use http_build_query with the new associative array we've built:

$queryString = http_build_query($associativeArray);

Result: username=my_username&email=my_email&password=12345678&confirm_password=12345678.

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

1 Comment

Thanks for the clarification.

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.