I'm working on a module to integrate with the Piryx API, and per the OAuth documentation, I should be redirected to an external page where the user can allow access to my application. The problem is that I'm using drupal_http_request():

$response = drupal_http_request($url, $headers, 'GET', $data);

to get the data, and the page markup is returned in $response['data'] instead of redirecting me to the external page (if I put the URL in my browser, it brings up the page fine). Is there something I can do to redirect to the external URL so the user can allow access, or do I just somehow need to use the markup to display it in my site?



In the case the request is redirect to a different URL, $response->code is 301, and $response->redirect_url is the redirection URL. If the request is redirect, you can call drupal_http_request() using $response->redirect_url as new URL.

See aggregator_aggregator_fetch() as example of Drupal function that verifies the request is being redirected.

  // Process HTTP response code.
  switch ($result->code) {
    case 304:
    case 301:
      $feed->url = $result->redirect_url;
      // Do not break here.
    case 200:
    case 302:
    case 307:
      if (!isset($result->data)) {
        $result->data = '';
      if (!isset($result->headers)) {
        $result->headers = array();
      $feed->source_string = $result->data;
      $feed->http_headers = $result->headers;
      watchdog('aggregator', 'The feed from %site seems to be broken, due to "%error".', array('%site' => $feed->title, '%error' => $result->code . ' ' . $result->error), WATCHDOG_WARNING);
      drupal_set_message(t('The feed from %site seems to be broken, because of error "%error".', array('%site' => $feed->title, '%error' => $result->code . ' ' . $result->error)));

If you want to redirect the users to $result->redirect_url, and not requesting that URL with drupal_http_request(), you use the following code.

drupal_goto($response->redirect_url, array('absolute' => TRUE));
  • OK, I'm confused. The response code I'm getting is actually 302. What I can't figure out is how to actually redirect the user to the location in $response->redirect_url, because I need the user the allow access to my application on that page. Calling drupal_http_requeset() again doesn't do that; it just gives me another response object. – wonder95 Oct 13 '11 at 13:45
  • If you look at aggregator_aggregator_fetch(), you will see that the code 302 is handled as if the drupal_http_request() returned 200. – kiamlaluno Jul 13 '12 at 1:52

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.