The first method is correct; you should not use url()
for an external URL that is passed to drupal_http_request()
, except (for example) when you want other modules to alter the hook via their hook_url_outbound_alter()
implementations, or you want to let url()
split off the fragment (or the query) from the URL. (The fragment is used to set $options['fragment'], when it's not already set; the query is added to
$options['query'].
The code run by url()
for an external URL is the following one. (I showed only the relevant code; where I omitted code, I used an // Omissis
comment. I also removed the original comments from the code and added mine to evidence some code parts.)
if (!isset($options['external'])) {
$options['external'] = $path === $_GET['q'] ? FALSE : url_is_external($path);
}
// Omissis
if ($options['external']) {
if (strpos($path, '#') !== FALSE) {
list($path, $old_fragment) = explode('#', $path, 2);
if (isset($old_fragment) && !$options['fragment']) {
$options['fragment'] = '#' . $old_fragment;
}
}
if ($options['query']) {
$path .= (strpos($path, '?') !== FALSE ? '&' : '?') . drupal_http_build_query($options['query']);
}
// Change the URL protocol basing on a Drupal local setting.
if (isset($options['https']) && variable_get('https', FALSE)) {
if ($options['https'] === TRUE) {
$path = str_replace('http://', 'https://', $path);
}
elseif ($options['https'] === FALSE) {
$path = str_replace('https://', 'http://', $path);
}
}
return $path . $options['fragment'];
}
Apart from what I reported before, there isn't much any pro in using url()
when used for a string you already know it contains an external URL. The part commented with Change the URL protocol basing on a Drupal local setting.
only changes the used protocol basing on the value of $options['https']
, when a local Drupal settings has been set to TRUE
. For a string that contains already an external URL, which is then passed to drupal_http_request()
, that code isn't much helpful; you would know already if the external URL needs to be accessed via https:// or http://, and you would not want to change the used protocol basing on a local setting.
application/x-www-form-urlencoded is the content type used when POSTing data, while you are GETting data.
To build the query data, you could also use drupal_http_build_query()
, which the same function used from url()
for the same purpose.
$data = array('name' => 'value', 'name1' => 'value1');
$options = array(
'method' => 'GET',
'data' => drupal_http_build_query($data),
'timeout' => 15,
);
$result = drupal_http_request('http://example.com/foo', $options);