6

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?

10

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>');
2
  • Much appreciated. I am part of the copy and paste generation :) Prefer that to long docs :P
    – giorgio79
    Oct 6 '11 at 7:49
  • 2
    The 'long docs' is what makes Drupal usable in the first place. You have 2 paragraphs explaining function definition and parameters, after that links to 14 code examples using that particular function, this is not long documentation friend. With all the ton of respect I have for SO, it is not a replacement for proper documentation. Much more info for you to digest in the docs, and you can be sure it's the real deal, for about the same amount of time. ( My 2 cents. ) SO is precious to me when the docs are unclear, or there is a void ... ;) Dec 17 '11 at 9:33
1

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.

0

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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