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.
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>
...
test.php
<?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);
?>
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.
print function function_name(..)
)