Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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