b2go Webservice API

You can call our b2go webservice directly from your web application to create nice short URLs.

How it works

Your application sends an XML to our web service, containing the long original URL. You will get an XML output with the generated short URL. The service is free to use. You don´t even need an API key or something.

Methods

  • createUrl
    to send your original (long) URL and get a short one back.
  • getUrl
    to receive the original (long) URL if you have a short b2go.li url.

SOAP XML schemata


PHP using SOAP

This is only available if PHP was configured with --enable-soap.
Needs at least php version 5.
Replace "$realurl" with the long URL you want to shorten.
The webservice returns the short url assigned to the variable "$short_url".

Sample Code
<?php
$client = new SoapClient('http://www.b2go.li/b2goapi.asmx?wsdl', array('soap_version' => SOAP_1_1));

$realurl = "http://www.example.com/foo.php?id=abcdef&session=hfsdgjkhfjhjhfbg4654&answer=42";

$short_url = $client->CreateUrl(array('real_url'=>$realurl))->CreateUrlResult;
$short_url = (array)$short_url;

echo $short_url["ShortenedUrl"];
?>

PHP using cURL

This is only available if PHP was compiled with cURL and SimpleXML extensions.
Needs at least php version 5.

Sample Code
<?php
function b2goCreateUrl($realurl){
$host = "http://www.b2go.li/b2goapi.asmx";
$xml_request = '<?xml version="1.0" encoding="utf-8"?>'."\n";
$xml_request .= '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'."\n";
$xml_request .= ' <soap:Body>'."\n";
$xml_request .= ' <CreateUrl xmlns="http://b2go.li/">'."\n";
$xml_request .= ' <real_url>'.$realurl.'</real_url>'."\n";
$xml_request .= ' </CreateUrl>'."\n";
$xml_request .= ' </soap:Body>'."\n";
$xml_request .= '</soap:Envelope>'."\n";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('POST /b2goapi.asmx HTTP/1.1','Host: b2go.li','Content-Type: text/xml; charset=utf-8','SOAPAction: "http://b2go.li/CreateUrl"',));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_request);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
$resultArray = curl_getinfo($ch);
curl_close($ch);
if($resultArray['http_code'] == "200"){
$b2go_result = $result;
} else {
$b2go_result = "Error posting to b2go. Retry";
}
return $b2go_result;
}
$realurl = "http://www.example.com/foo.php?id=abcdef&session=hfsdgjkhfjhjhfbg4654&answer=42";
$xmlString = b2goCreateUrl($realurl);
$xmlString = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $xmlString);
$xml = simplexml_load_string($xmlString);
$short_url = $xml->soapBody->CreateUrlResponse ->CreateUrlResult->ShortenedUrl;
echo $short_url;
?>

ASP.Net using C#

If you are not familiar implementing Web Services in Microsoft Visual Studio, please read this doc:
http://msdn.microsoft.com/en-us/library/t745kdsh.aspx
Replace "sLongURL" with the long URL you want to shorten.
The webservice returns the short url assigned to the variable "sShortURL".

Sample Code
try
{
string sLongURL = "http://www.example.com/foo.php?id=abcdef&session=hfsdgjkhfjhjhfbg4654&answer=42",
sShortURL = "";

Bob2GoAPI.ShortURL oShortURL = new ShortURL();
b2goapiServiceSoapClient oWebservice = new
b2goapiServiceSoapClient(new BasicHttpBinding(), new
EndpointAddress("http://b2go.li/b2goapi.asmx"));

oShortURL = oWebservice.CreateUrl(sLongURL);
sShortURL = oShortURL.ShortenedUrl;
}
catch(Exception ex)
{
// your exception handling
}