Wednesday 14 January 2015

How to retrieve data using Drupal_http_response

Ever wanted to access an API using Drupal. It’s dead easy using drupal_http_response().


 <?php  
  // Construct parameters to pass to URL  
  $data   = 'param_name=value&'param_name1=value1';  
  $options = array(  
   'method' => 'POST', // HTTP Request  
   'data'    => $data,  // Params  
   'headers' => array('Content-Type' => 'application/json'), // Data retrieved type  
  );  
  // Get result  
  $result = drupal_http_request('http://test.com', $options);  
 ?>  


Tada! This syntax be used for your AJAX requests or API calls in Drupal. Easy peezy!

No comments:

Post a Comment