I want to do a post request to an HTTPS server address... I have this example code that demonstrates sending a POST request -

$data = 'name=value&name1=value1';

$options = array(
                'method' => 'POST',
                'data' => $data,
                'timeout' => 15,
                'headers' => array('Content-Type' => 'application/x-www-form-urlencoded'),

$result = drupal_http_request('http://somewhere.com', $options);

and also on drupal_http_request, there is this code -

case 'https':
  // ***************
  // Note: Only works when PHP is compiled with OpenSSL support.
  // ***************
  $port = isset($uri['port']) ? $uri['port'] : 443;
  $socket = 'ssl://' . $uri['host'] . ':' . $port;
  $options['headers']['Host'] = $uri['host'] . ($port != 443 ? ':' . $port : '');

to demonstrate sending a POST to an HTTPS connection. Problem is, I can't figure out out to implement the HTTPS options in the POST example code above. Can someone please help me and explain how to do this? I'm quite new to PHP code writing and Drupal and could definitely use the guidance. The drupal_http_request documentation are great as an index, but it doesn't help you a lot if you want to learn things from scratch.


after your comment's, i got to this code -

    $data = 'access_token=455754fhnaI&href=fb&template=You have people waiting to play with you, play now!';

$options = array(
                'method' => 'POST',
                'data' => $data,
                'timeout' => 15,
                'headers' => array('Content-Type' => 'application/x-www-form-urlencoded'),

$result = drupal_http_request('https://graph.facebook.com/1000721/notifications?', $options);

but its still dosen't work... if i post via FIREFOX poster with this patten -

     href= fb & 
     template=You have people waiting to play with you, play now!

it's working... so i am probably not constructing the request properly in drupal... thanks a lot everyone... really hope someone could help me figure this out...

  • Have you tried $result = drupal_http_request('https://somewhere.com', $options);?
    – Clive
    Dec 17, 2012 at 14:52
  • 4
    You should also really use drupal_http_build_query
    – mpdonadio
    Dec 17, 2012 at 15:14

2 Answers 2


You should only need to set it in the protocol.

$result = drupal_http_request('https://somewhere.com', $options);


$url: A string containing a fully qualified URI.

array $options: (optional) An array that can have one or more of the following elements:

  • headers: An array containing request headers to send as name/value pairs.
  • method: A string containing the request method. Defaults to 'GET'.
  • data: A string containing the request body, formatted as 'param=value&param=value&...'. Defaults to NULL.
  • max_redirects: An integer representing how many times a redirect may be followed. Defaults to 3.
  • timeout: A float representing the maximum number of seconds the function call may take. The default is 30 seconds. If a timeout occurs, the error code is set to the HTTP_REQUEST_TIMEOUT constant.
  • context: A context resource created with stream_context_create().
  • thanks a lot both of you... I will try it and come back with results...
    – BLV
    Dec 17, 2012 at 14:58
  • i still couldn't get it working... could you guys please check out my edit and tell me what you think?
    – BLV
    Dec 17, 2012 at 15:15
  • I don't see a problem with it. you could try removing the "?" from your URL, though I doubt that's the problem. this could be a facebook issue? authentication problem or header request issue?
    – jdu
    Dec 17, 2012 at 16:24
  • i got it... the problem was i had to put the access_token as part of the URL, and not as a part of the header parameters as i did by mistake...
    – BLV
    Dec 17, 2012 at 16:45
  • I just ran into this. So counter-intuitive. Apr 9, 2014 at 23:12

Alternative to this is to use HTTPRL. Try replacing drupal_http_request with httprl_override_core. If it works, there is a setting admin/config/development/httprl you can set in the GUI to make core use HTTPRL.

When using HTTPRL you don't need to manually encode the $data. Leave it as an array (skip http_build_query).

  • That module looks grate.
    – Roy Segall
    Mar 2, 2014 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.