Skip to main content
3 of 8
deleted 605 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 that you can find here http://pastebin.com/cXLe6R35

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 shortlink is created and works fine.

But when I navigate to a news node, the script runs ok but google responds with a 403 (forbidden) error.

It's the exact same script!!! When called from outside drupal works fine, inside it doesn't.

I'm really scratching my head over this. And I've been around this for 3 days!!!! Any help is appreciated.

Tivie
  • 305
  • 3
  • 17