开发者

PHP urlencode / urldecode of date(DATE_ATOM) issue

开发者 https://www.devze.com 2023-01-14 18:28 出处:网络
I am producing a google calendar query string that requires an atom fomatted date. I am using php 5.1.6, and date(DATE_ATOM) to produce the correctly formatted current date. So, for instance, in the

I am producing a google calendar query string that requires an atom fomatted date.

I am using php 5.1.6, and date(DATE_ATOM) to produce the correctly formatted current date. So, for instance, in the unencoded url part of the query has:

start-max=2010-09-02T10:25:58+01:00

I need to rawurlencode this and it becomes

start-max%3D2010-09-02T11%253A37%253A59%252B01%253A00 

Now if I rawurldecode this it becomes

start-max=2010-09-02T11%3A39%3A35%2B01%3A00

So it hasn't decoded properly and google rejects the query...

If I rawurldecode the query twice the date is decoded but the original '+' is replaced with a space (开发者_StackOverflow中文版 even though it is still encoded in the above string)

The same is true for urlencode/urldecode :(

Any ideas how to encode / decode the URL with this date format in it?

Cheers


Cannot reproduce:

$ php -r 'echo rawurlencode("start-max=2010-09-02T10:25:58+01:00").PHP_EOL;'
start-max%3D2010-09-02T10%3A25%3A58%2B01%3A00
$ php -r 'echo rawurlencode(rawurlencode("start-max=2010-09-02T10:25:58+01:00")).PHP_EOL;'
start-max%253D2010-09-02T10%253A25%253A58%252B01%253A00

$ php -r 'echo rawurldecode(rawurlencode("start-max=2010-09-02T10:25:58+01:00")).PHP_EOL;'
start-max=2010-09-02T10:25:58+01:00
$ php -r 'echo urldecode(urlencode("start-max=2010-09-02T10:25:58+01:00")).PHP_EOL;'
start-max=2010-09-02T10:25:58+01:00

So, probably you're rawurlencode'ing the value, and then the whole string:

$ php -r 'echo rawurlencode("start-max=".rawurlencode("2010-09-02T10:25:58+01:00")).PHP_EOL;'
start-max%3D2010-09-02T10%253A25%253A58%252B01%253A00

...which could be desired behavior if you're sending a complete url in an get variable, but more probably you have a logical error somewhere.


You must not encode the =-symbol in your query string - only the parameter value itself and the parameter name (if it's not fixed and could contain problematic characters) should be urlencoded. The correct way would be

$query = 'start-max='.urlencode(date(DATE_ATOM));
// or if the parameter name could be problematic
$query = urlencode('start-max').'='.urlencode(date(DATE_ATOM));


Use PHP's http_build_query:

$BaseURL = 'http://example.com/page.php';
$Query   = http_build_query(array(
    'start-max'=>'2010-09-02T10:25:58+01:00',
    'param2'=>'anotherval',
));

$URL = $BaseURL. '?'. $Query;
0

精彩评论

暂无评论...
验证码 换一张
取 消