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.

CommentFileSizeAuthor
#1 drupal_http_request_fragment-116330.patch461 bytescorbacho

Comments

corbacho’s picture

StatusFileSize
new461 bytes

Patch

corbacho’s picture

Status: Active » Needs review

.

bfroehle’s picture

Category: bug » support
Status: Needs review » Fixed

corbacho: Instead, you'll need to pass a valid query string. See drupal_http_build_query() and url().

For example, your code could be:

$url = 'http://drupal.org/node/1015704#comment-3905632'
$response = drupal_http_request(url("http://tinyurl.com/api-create.php", array('query' => array('url' => $url))));
if ($response->code == 200) {
  return $response->data;
}
corbacho’s picture

thanks ;) and this also works in Drupal 6.x. I will submit a patch for twitter module then

corbacho’s picture

Status: Fixed » Active

UPDATED.
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:

$url = url('http://drupal.org/node/1015704', array('fragment' => 'comment-3905632'));

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.

corbacho’s picture

Status: Active » Closed (fixed)

Ignore 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.