views:

128

answers:

2

How do I implement is.gd's URL shortening API in my iPhone Application?

+1  A: 

There is a tutorial here for using tr.im url shorteners. Presumably apis are fairly similar for the two, so just read the API docs and implement in a similar way. Alternatively just use tr.im with the provided code.

Jack
This tutorial is good and I belive that I use it also for is.gd... Thanks!
Matthew
+1  A: 

You can use the CFHTTP API to create an HTTP request. This allows you to easily invoke HTTP GET. Use it to send the following request where the longurl is the URL you wish to shorten.

http://is.gd/api.php?longurl=http://www.example.com

You will receive back a response header of "HTTP/1.1 200 OK" if the URL was shortened as expected, or "HTTP/1.1 500 Internal Server Error" if there was a problem. The body of the response will contain the shortened URL in plain text if everything was successful. If the request was unsuccessful, the body of the response will contain a specific error message.

Your request might looking something like this...

CFStringRef requestHeader = CFSTR("Connection");
CFStringRef requestHeaderValue = CFSTR("close");
CFStringRef requestBody = CFSTR("");

CFStringRef url = CFSTR("http://is.gd/api.php?longurl=http://www.example.com");
CFStringRef requestMethod = CFSTR("GET");

CFURLRef requestURL = CFURLCreateWithString(kCFAllocatorDefault, url, NULL);
CFHTTPMessageRef request = CFHTTPMessageCreateRequest(kCFAllocatorDefault,
    requestMethod, requestURL, kCFHTTPVersion1_1);
CFHTTPMessageSetBody(request, requestBody);
CFHTTPMessageSetHeaderFieldValue(request, requestHeader, requestHeaderValue);

CFDataRef serializedRequest = CFHTTPMessageCopySerializedMessage(request);
pdavis