Skip to content

Instantly share code, notes, and snippets.

@mgng
Last active October 2, 2015 06:58
Show Gist options
  • Select an option

  • Save mgng/2195827 to your computer and use it in GitHub Desktop.

Select an option

Save mgng/2195827 to your computer and use it in GitHub Desktop.
twitterのアイコン画像を取得する
<?php
// http://mgng.aws.af.cm/twicon
$id = isset( $_GET['id'] ) ? $_GET['id'] : '';
$size = isset( $_GET['size'] ) ? $_GET['size'] : '';
if ( !preg_match( '/\A[\w\-]+\z/u', $id ) ) {
header('Not Found', true, 404);
exit;
}
if ( !preg_match( '/\A(mini|normal|bigger|org)\z/', $size) ) {
$size = 'normal';
}
$src = @file_get_contents(
"http://twitter.com/{$id}",
false,
stream_context_create (
array(
'http' => array(
'method' => 'GET',
'header' => implode( "\r\n", array(
'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0',
'Referer: https://twitter.com/',
)) . "\r\n",
)
)
)
);
if ( $src === false ) {
header('Not Found', true, 404);
exit;
}
preg_match( '/src=".+\/\/(.+?\/(default_|)profile_images\/.+?)"/u', $src, $m );
if ( !isset( $m[1] ) ) {
header( 'Not Found', true, 404 );
exit;
}
$redirect_img = 'http://' . preg_replace( '/_(small|mini|normal|bigger|org)+/u', ($size === 'org') ? '' : "_{$size}" , $m[1]);
header( "Cache-Control: max-age=3600" );
header( "Location: {$redirect_img}" );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment