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

  // 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 {

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

  //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 {

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

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

<div class="test"><?php print drapps_shortlinks_createlink($url, TRUE, FALSE); ?></div>



$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);


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.

  • This is not an answer but I cannot post comments: I'm looking at your and google's code and I cannot figure it out (I'm too lazzy to test myself) could be an encoding problem maybe?
    – Pierre
    May 22 '11 at 20:12
  • by encoding you mean file encoding? I've set all files to UTF-8 without BOOM.
    – Tivie
    May 22 '11 at 20:56
  • 4
    Can you include how you exactly call that function both in the separate file and inside Drupal? Because what you posted is not valid PHP... (print function function_name(..))
    – Berdir
    Jun 2 '11 at 19:18
  • 2
    Are you sure include_once('ShortLink.php'); works as expected? Is this file really in your root of your drupal directory? Jun 3 '11 at 14:07
  • 2
    In the spirit of brainstorming, my first thought is that perhaps Drupal sets some globals which share a namespace with those in ShortLink - $user in particular raises flags, even though you are not initiating the global Drupal $user object in your code. Try changing a few of those variable names and seeing if you still have issues.
    – cam8001
    Jun 3 '11 at 14:25

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.

  • Followed your sugestion using module_load_include. Same thing. The file is being loaded correctly but google responds with 403 forbidden. The included file is a class. I removed the 2 functions in the \ namespace but it made no difference.
    – Tivie
    Oct 18 '12 at 0:36

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.