Created
January 19, 2012 16:54
-
-
Save abackstrom/1641150 to your computer and use it in GitHub Desktop.
Fetch short URL using git.io
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class GitHub_URL { | |
protected $long; | |
public function __construct( $url ) { | |
$this->long = $url; | |
} | |
public function long() { | |
return $this->long; | |
} | |
public function short() { | |
$ch = curl_init( 'http://git.io/' ); | |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); | |
curl_setopt( $ch, CURLOPT_POSTFIELDS, array( 'url' => $this->long() ) ); | |
curl_setopt( $ch, CURLOPT_HEADER, true ); | |
$response = curl_exec( $ch ); | |
$headers = explode( "\r\n", $response ); | |
foreach( $headers as $header ) { | |
if( false === strpos( $header, ': ' ) ) { | |
continue; | |
} | |
list( $key, $value ) = explode( ': ', $header, 2 ); | |
if( 'Location' === $key ) { | |
return $value; | |
} | |
} | |
} | |
public function __toString() { | |
return $this->long(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment