Example use case: You want to shorten a url using the API of some third-party service:
$url = 'http://drupal.org/node/1015704#comment-3905632'
$response = drupal_http_request("http://tinyurl.com/api-create.php?url=" . $url);
if ($response->code == 200) {
return $response->data;
}
(this code is from twitter.module)
the short URL will point to http://drupal.org/node/1015704, because drupal_http_request will not include the fragment (anchor).
Inside of drupal_http_request it's used the php function $uri = parse_url(), so it's only matter of using the $uri['fragment'] component and append it to the $path.
Comments
Comment #1
corbacho commentedPatch
Comment #2
corbacho commented.
Comment #3
bfroehle commentedcorbacho: Instead, you'll need to pass a valid query string. See drupal_http_build_query() and url().
For example, your code could be:
Comment #4
corbacho commentedthanks ;) and this also works in Drupal 6.x. I will submit a patch for twitter module then
Comment #5
corbacho commentedUPDATED.
I open it again because that it's not working.
In the first example I put a string value $url to simplify, but in fact I was using url() with the 'fragment' option. Something similar to this:
This is the right way to do it. Because if it the anchor is in the 'query' option, then it will be encoded as %2523, and anchor will stop working (before or after shorten it). There is a similar issue in the menu system #123103: Retain #anchors during path alias -> normal path saving
Conclusion: I would like the patch I submitted to be considered again. I think the cleanest solution is to make drupal_http_request aware of the
$uri['fragment']part that parse_url is returning.Comment #6
corbacho commentedIgnore my last update.
Thanks again bfroehle, it's working flawless the way you said. Even it's encoded the # character, tinyurl will recognize it. And with the patch I submitted will not even work, tinyurl will not include the anchor in the short url.