0

I am trying to build a url with a query using the url() function.

    $options = array(
        'query' => array(
                'parameter1' => 'value1',
                'parameter2' => 'value1,value2',
        ),
        'absolute' => TRUE,
        'external' => TRUE,
    );
    url ('https://somesite.com/', $options);

The URL becomes: https://somesite.com/?parameter1=value1&parameter2=value1%2Cvalue2

So commas become encoded as %2C.

How can I build a query that has parameters with multiple values separated by comma?

EDIT: I have to deal with an old API that does not recognize %2C as comma characters, so actual commas have to be present in the end url.

  • That's actually correct. Symbols on url parameters are supposed to be encoded on a correctly formatted url. On the server side, they should arrive as commas. – Joseph May 17 '17 at 12:41
  • But I have to deal with an old API which does not decode %2C into commas... so actual commas need to be in the URL.. what i ended up doing is a str_replace on the result of the url() function... but that is very ugly – dasj19 May 17 '17 at 12:46
  • Then you must mention that (and any other detail) in the question. Related reading: meta.stackexchange.com/questions/66377/what-is-the-xy-problem – Joseph May 17 '17 at 12:47
1

How can I build a query that has parameters with multiple values separated by comma?

You already have, the commas are just encoded so they can be transported safely as part of the URL.

If you mean how can you stop the url() function from encoding special characters: you can't, without editing it. More specifically, you would need to replace its use of drupal_http_build_query with something that only selectively encodes special characters, much like drupal_http_build_query does itself for the / character.

The other options you have are

  • Do a string replace on the return value from url (or parse it and build it up again to your own spec)
  • Roll your own url function to your own spec
  • Contact the maintainers of the API and ask them to fix their decoding bug. Obviously YMMV on that.
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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