Question

Would love to see some examples on getting remote webpage content with drupal http request. I have plenty of examples easily with Google with curl, but yet to find one with this drupal function.

Also, would CURL be better for getting remote content?

Was it helpful?

Solution

First, you can read the documentation about drupal_http_request() its pretty well documented and it will help you.

But for simple application, you can just do it like that.

<?php
$result = drupal_http_request('http://google.com/');
if (in_array( $result->code, array(200, 304))) {
  // Use $result->data for the content
}
else {
  // Error $result->code
}

// Also you can see all content from $result with
// if you had installed Devel Module and enable it
// dsm($result)
// or simply like that drupal_set_message('<pre>' . print_r($result, 1) . '</pre>');

OTHER TIPS

You could also try using the Webclient module (for D7). Here is a quote about it (from the module's project page):

This is a library module. It provides no out of the box functionality other then providing an API that other modules can use.

The Webclient API is seperated into three major components:

  • Webclient
  • Webclient Formatters
  • Webclient Proxy

All of the above components were translated to seperate modules so that developers can create a dependency with the functionality they need.

Im using this code without any problem.

$base_url = 'http://route to Ws';
$data = array(
  'key' => 'foo';
  'mail' => $mail,
);

$data = drupal_json_encode($data);
$options = array(
'headers' => array(
  'Content-Type' => 'application/json',
),
'method' => 'POST',
'data' => $data
);
$response = drupal_http_request($base_url, $options);
$data = json_decode($response->data);
// Check if login was successful
if ($response->status == 'valid' {
return TRUE;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top