Skip to main content
6 of 8
added 428 characters in body
Tivie
  • 305
  • 3
  • 17

Calling a PHP script from Drupal retrieves different result than calling it outside Drupal

I'm trying to create a module that creates a Shortlink using Google Apps Shortlink Api (undocumented since its still in labs).

Here's the script (also at http://drupalbin.com/18667):

function drapps_shortlinks_createlink($url, $verbose = FALSE, $bypass_validattion = FALSE) {
  //loads ShortLink Class
  include_once('ShortLink.php');
       
  // URL validation with 2 clauses:
  // 1 - FILTER_VALIDATE_URL -> tests if the url is well constructed
  // 2 - @fopen -> tests if the URL actually exist
  if (!filter_var($url, FILTER_VALIDATE_URL) OR !@fopen($url,"r")) {
    // URL not valid or inexistant, so bail
    if ($verbose) {
      $msg = 'Invalid URL';
      return $msg;
    }
    else {
      return;
    }
  }
       
  // Get configuration values
  $domain = 'mydomain.com';
  $key = 'someHMACKEY';
  $user = '[email protected]';
     
  // Check if user admin has configured DRAPPS and DRAPPS ShortLinks
  if (!$domain OR !$key OR !$user) {
    //Something is missing so bail
    if ($verbose) {
      $msg = 'DRAPPS Shortlinks is not configured. Please go to <a href="' . base_path() . 'admin/settings/drapps">admin/settings/drapps</a>';
      return $msg;
    }
    else {
      return;
    }
  }
 
  //PHP Google Apps ShortLinks Implementation
  $slink = new ShortLink($domain, $key, $user);
  $r = clone $slink->pub($url);
  $status = $r->status;

  if ($status == 'ok') {
    return $r;
  }
  elseif ($verbose) {
    return $status;
  }
  else {
    return;
  }
}

This script uses a custom created class called SHortLink.php.

I also created a test.php, located at mydomain.com/test/test.php for testing purposes with this line <?php print function drapps_shortlinks_createlink($url, TRUE, FALSE); ?> and changed node-news.tpl.php inserting the same line in a div (also for testing purposes).
When I go to http://www.mydomain.com/test/test.php, the script works fine and I get an 'ok' response from google; a short link is created and it works fine. When I navigate to a news node, the script runs OK, but Google responds with a 403 (forbidden) error.

EDIT_1: THIS SCRIPT WORKS. So i think I can safely assume that the reson behind this has something to do with drupal rather than the script itself.

EDIT_2: I think the real question behind this is: What reasons could make a script behave differently when called from drupal? Any input will be appreciated: brainstorming, ideas, tips, troubleshooting methods, anything you think that might help, I will really appreciate it.

Tivie
  • 305
  • 3
  • 17