Skip to content

Instantly share code, notes, and snippets.

@abackstrom
Created January 19, 2012 16:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abackstrom/1641150 to your computer and use it in GitHub Desktop.
Save abackstrom/1641150 to your computer and use it in GitHub Desktop.
Fetch short URL using git.io
<?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