Recently I was working on a project where I was wanted to make a request to another website to submit data from Drupal site. PHP provides very useful method for it i.e. cURL. You can use cURL in Drupal as well but Drupal provides its own way of making HTTP Request i.e. drupal_http_request($url, array $options = array()).

Here's how to use drupal_http_request() to make HTTP Request:

<?php
  $data
= 'name=value&name1=value1'; // Parameters to pass to URL
 
$options = array(
   
'method' => 'POST', // HTTP Request Type
   
'data' => $data, // Parameters
   
'headers' => array('Content-Type' => 'application/x-www-form-urlencoded'),
  );
 
$result = drupal_http_request('<a href="http://example.com'">http://example.com'</a>, $options); // Make request
?>

The function drupal_http_request($url, array $options = array()) takes two arguments, first is URL to make request and second is an array of values (optional) to make request such as method type, parameters to pass to URL, headers, etc.

Reference:

Tags: