Rage9 already nailed it, but I posted this anyway. I'm a C#/C++ dev picking up PHP.
Copy and paste this into NotePad++ or whatever PHP editor you're using:
<?php
// In this example, we'll pretend the call looks like this: untinyUrl('3xutzf3');
function untinyUrl($tinyurl){
// STEP #1.) Open a socket to port 80 (standard Web) with a 30-second timeout
// If an error occurs, store the Error Number in $errno and the Error Message in $errstr
// Notice: there's not error handling done anywhere, as this is the first and last usage
// of the err variables
if($fp = fsockopen ("tinyurl.com", 80, $errno, $errstr, 30)){
// STEP #2.) If socket was opened successfully, it will return a reference to the socket
// which can be used exactly like a filestream (using fputs)
if ($fp) {
// STEP #3.) This issues an HTTP HEAD request by writing to the stream.
// The request will actually look something like this:
//
// HEAD /3xutzf3 HTTP/1.0
// Host: tinyurl.com
//
fputs ($fp, "HEAD /$tinyurl HTTP/1.0\r\nHost: tinyurl.com\r\n\r\n");
// STEP #4.) After an fputs command, the $fp contains the response from the host
// This while loop cycles through the responds and dumps it into the "headers"
// variable in chunks of 128 bytes
$headers = '';
while (!feof($fp)) {
$headers .= fgets ($fp,128);
}
// Close the socket
fclose ($fp);
}
// STEP #5.) Parse the response headers
// This is what the response looks like in raw form:
// HTTP/1.0 301
// Moved Permanently
// X-Powered-By: PHP/5.3.3
// Location:
http://phpsnippets.info
// X-tiny: cache 0.00046706199645996
// Content-type: text/html
// Connection: close
// Date: Sat, 16 Oct 2010 20:01:56 GMT
// Server: TinyURL/1.6
// The result of this explode is an array with two elements
// The two halves of the string separated by "Location:"
$arr1=explode("Location:", $headers);
// $arr1[0] contains this:
// HTTP/1.0 301
// Moved Permanently
// X-Powered-By: PHP/5.3.3
// $arr1[1] contains this:
//
http://phpsnippets.info
// X-tiny: cache 0.00046706199645996
// Content-type: text/html
// Connection: close
// Date: Sat, 16 Oct 2010 20:01:56 GMT
// Server: TinyURL/1.6
// This explode separates the second half ( $arr1[1] ) by all of the newlines (\n)
$arr=explode("\n",trim($arr1[1]));
// $arr[0] contains this:
//
http://phpsnippets.info
echo trim($arr[0]);
}
}
echo untinyUrl('3xutzf3');
?>
If you want to look at it from another angle:
1.) Grab a copy of Fiddler (it's free) ==>
Fiddler Web Debugger - Freeware HTTP(S) debugging tool.
2.) Click on the "Request Builder" tab
3.) Choose "HEAD" and enter "http://tinyurl.com/3xutzf3" as the URL and choose "HTTP/1.0" from the last drop down list
4.) Click Execute
5.) If look in the "Raw" tab from the Request Builder, it's the same as the request above
6.) Double click the Request in the "Web Sessions" -- this will move from the "Request Builder" to the "Inspector". You should see the response with "http://phpsnippets.info"
7.) Click the "Raw" Tab right above "Response Headers" and you'll see:
HTTP/1.0 301 Moved Permanently
X-Powered-By: PHP/5.3.3
Location:
http://phpsnippets.info
X-tiny: cache 0.00040102005004883
Content-type: text/html
Connection: close
Date: Sat, 16 Oct 2010 20:31:13 GMT
Server: TinyURL/1.6
Voila: same thing as above.