Hey, i'm trying to make use of the google URL Shortener API, and so far I'm having a bit of trouble.
Here is the output from my script (quoted from the console):
Data is Object: false
Data:
%data:
Connecting to: www.googleapis.com:80/urlshortener/v1/url?fields=id&pp=1&key=AIzaSyBEwWcD_igyt6Zw_n94cPnusFk5nZ-DQLo
QUERY: POST /urlshortener/v1/url?fields=id&pp=1&key=AIzaSyBEwWcD_igyt6Zw_n94cPnusFk5nZ-DQLo
Content-Type: application/json
Content-Length: 27
{
"longUrl":"www.www.com"
}
HTTP/1.0 411 Length Required
Content-Type: text/html; charset=UTF-8
Content-Length: 11791
Date: Wed, 07 Dec 2011 23:12:28 GMT
Server: GFE/2.0
<!DOCTYPE html>
<html lang=en>
<meta charset=utf-8>
<title>Error 411 (Length Required)!!1</title>
After it says that, blockland closes.
And here is the code:
$MAX_HTTP_QUERY_STRING = 255;
function httpPage2::init(%this, %url)
{
%host = "";
%page = "";
if(strpos(%url, "http://") == 0)
{
%host = getSubStr(%url, 7, strpos(%url, "/", 8) - 7);
%page = getSubStr(%url, strpos(%url, "/", 8), $MAX_HTTP_QUERY_STRING);
}
else
{
%host = getSubStr(%url, 0, strpos(%url, "/", 8));
%page = getSubStr(%url, strpos(%url, "/"));
}
if(strpos(%host, ":") < 0) %host = %host @ ":" @ "80";
%this.Address = %host;
%this.Page = %page;
}
function httpPage2::get(%this, %url)
{
%this.Buffer = "";
%this.doBuffer = false;
%this.init(%url);
// warn("Connecting to: " @ %this.Address @ %this.Page);
%this.Method = "GET";
%this.connect(%this.Address);
}
function httpPage2::post(%this, %url, %data)
{
%this.Data = "";
if(isObject(%data))
{
warn("Data is Object: true");
for(%x = 0; %x < %data.getCount(); %x++)
{
%datum = %data.getObject(%x);
if(strlen(%postData) > 0)
%postData = %postData @ "&";
%this.Data = "\"" @%datum.key @ "=" @ %datum.value;
}
}
else
{
warn("Data is Object: false");
%this.Data = %data;
}
echo("\c0Data: " @ %this.Data);
echo("\c0%data: " @ %data);
%this.init(%url);
warn("Connecting to: " @ %this.Address @ %this.Page);
%this.Method = "POST";
%this.connect(%this.Address);
}
function httpPage2::onConnected(%this)
{
// warn("Connected ...");
%query = %this.Method @ " " @ %this.page;
if(%this.Method $= "POST")
{
%query = %query @ "\n\n" @ "Content-Type: application/json\n";
%query = %query @ "Content-Length: " @ strlen("{\n\"longUrl\":\"" @ $url @ "\"\n}") @ "\n\n";
%query = %query @ "{\n\"longUrl\":\"" @ $url @ "\"\n}";
}
else
{
%query = %query @ "\n\n";
}
warn("QUERY: " @ %query);
%this.send(%query);
}
function httpPage2::onLine(%this, $line)
{
echo($line);
if(!%this.doBuffer && $line $= "")
{
%this.doBuffer = true;
return;
}
if(%this.doBuffer)
{
if(%this.Buffer !$= "")
%this.Buffer = %this.Buffer;
%this.Buffer = %this.Buffer @ $line;
}
return $line;
}
function httpPage2::getResult(%this)
{
return $line;
}
function httpPage2::onDisconnect(%this)
{
// warn("Disconnected: " @ %this.Address);
}
function httpPage2::onConnectFailed(%this)
{
echo("<color:FFFFFF>Connection Failed: " @ %this.Address);
}
function Rab($url)
{
$url = "www.www.com";
new TCPObject(httpPage2) { };
httpPage2.post("http://www.googleapis.com/urlshortener/v1/url?fields=id&pp=1&key=AIzaSyBEwWcD_igyt6Zw_n94cPnusFk5nZ-DQLo");
echo(httpPage2.getResult());
}
Rab is the function to send the request.
Here is the google API explorer, directly linked to the correct section.
https://code.google.com/apis/explorer/#_s=urlshortener&_v=v1&_m=url.insertI honestly have no idea what i'm doing wrong, I got it to work for getting numbers from Random.org, and i'm very new to TCPObjects, so any help is appriciated.