Using drupal_http_request() with GET query-string parameters

Public
</> CopyGet raw version
php
  1. $query = array(
  2. 'variable1' => $variable1,
  3. 'variable2' => $variable2,
  4. );
  5.  
  6. $res = drupal_http_request('http://example.com?' . drupal_http_build_query($query));

Comments

doubouil's picture

Is there a specific reason for not using url() ?

$res = drupal_http_request( url('<a href="http://example.com'">http://example.com'</a>, array("query" => $query ) );

kalman.hosszu's picture

EDIT: dropbucket change url's to links.

You can use url() of course!

Otherwise the goal of the example is that you have to build the query instead of pass them to drupal_http_request() function, because it won't work:

  1. $query = array(
  2. 'variable1' => $variable1,
  3. 'variable2' => $variable2,
  4. );
  5. $params = array(
  6. 'method' => 'GET',
  7. 'data' => drupal_http_build_query($query),
  8. );
  9.  
  10. $res = drupal_http_request('<a href="http://example.com'">http://example.com'</a>, $params);
Ravi Karnati's picture

Thanks for the post.

$query = array(
'id' => 'iEEUTwGRDRw',
'part' => 'contentDetails,statistics',
'key' => 'AIzaSyC6yl76jJHfooqbEzpJJoqu19nqlpv5dp4',
);
$res = drupal_http_request('https://www.googleapis.com/youtube/v3/videos?' . drupal_http_build_query($query));

My Above code returns nothing. Kindly let me know the reason ? where am wrong ?

Thanks in Advance.

Ravi