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 -&gt; tests if the url is well constructed
  // 2 - @fopen -&gt; 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 = 'username@mydomain.com';

  // 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 &lt;a href="' . base_path() . 'admin/settings/drapps"&gt;admin/settings/drapps&lt;/a&gt;';
      return $msg;
    }
    else {
      return;
    }
  }

  //PHP Google Apps ShortLinks Implementation
  $slink = new ShortLink($domain, $key, $user);
  $r = clone $slink-&gt;pub($url);
  $status = $r-&gt;status;

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


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

EDIT: This function is called in Drupal's node-news.tpl.php like this:

...
&lt;div class="test"&gt;&lt;?php print drapps_shortlinks_createlink($url, TRUE, FALSE); ?&gt;&lt;/div&gt;
...


test.php

&lt;?php

$path = "path/to/drupal"; //changed for security reasons

include_once($path . 'sites/all/modules/drapps_shortlinks/drapps_shortlinks.inc');

$url = 'http://www.google.com/';

print drapps_shortlinks_createlink($url, TRUE, FALSE);

?&gt;


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.
You really shouldn't use include_once, Instead you should use module_load_include.

After you've done this start putting in Debug code in your include file to make sure it's being loaded. 

Also makes your your include file contains namespaces functions and no variable being used outside a function scope. To avoid any naming collisions that could happend.

Besides that, there really is not much help anyone can provide. A script will work the same way in Drupal as it would when being include in a regular php file.