views:

67

answers:

3

Hi

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 ( 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

A: 

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));
Stefan Gehrig
Good point. I was using http_build_query in the actual code, so I was slightly sloppy in my example above.
xymod
A: 

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.

Wrikken
Cheers - you made me go away and examine my code again and I believe I have been overzealous with my encoding. But its still not resolved: I will need to go and have another crawl through the code to see whats happening. I may have misinterpreted the google date requirements because its now interpreting the + as a space in the date format
xymod
ps - I am using the URL as a parameter value elsewhere in the code, but also as a URL for simplepie... the URL is initially encoded, and I am attempting to decode it prior to entry into simplepie. I was indeed encoding twice - once in the http_build_query, and then the complete URL
xymod
A: 

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;
aularon
Hi - thanks - but I was actually using this already, but was stupidly re-encoding the values in a function elsewhere and hadn't noticed.
xymod