<?
header( "Content-Type: text/html; Charset=utf-8" );
header( "pragma: no-cache" );
header( "Expires: Wed, 31 May 2000 14:59:58 GMT" );
header( "Cache-control: no-cache" );
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>URL短縮</title>
<style type="text/css">
* {
font-size:14px
}
</style>
<script type="text/javascript">
function setData() {
if ( document.getElementById("url").value == '' ) {
alert("URLが指定されていません ");
return false;
}
return true;
}
</script>
</head>
<body>
<div style='padding:10px;'>
<input type="button" value="閉じる" style='float:right' onclick='parent.document.getElementById("ggurl").style.display="none"'>
<br>
<form
id="frm"
method="post"
style='display:inline;'
onsubmit='return setData();'
>
<table>
<tr>
<td>URL</td>
<td><textarea readonly id="url" name="url" style='width:390px;height:200px;'><?= $_POST['url'] ?></textarea></td>
<tr>
<td colspan=2>
<input
type="submit"
value="URL短縮"
/>
</td>
<tr>
<td>短縮後</td>
<td>
<input
readonly
type="text"
id="text"
name="text"
style='width:390px;'
onclick='this.select();'
>
</td>
<tr>
</table>
</form>
<?
if ( strtoupper( $_SERVER['REQUEST_METHOD'] ) == 'POST' ) {
$url = googleShorten( $_POST['url'] );
$url = str_replace( "\n", "", $url );
$url = str_replace( "'", "\\'", $url );
?>
<script type="text/javascript">
var shorten = eval("(" + '<?=$url?>' + ")" );
document.getElementById("text").value = shorten.id;
</script>
<? } ?>
</div>
</body>
</html>
<?
function googleShorten( $url ) {
// *********************************************************
// curl 処理
// *********************************************************
$curl = curl_init();
// *********************************************************
// https 用
// *********************************************************
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
$api_url = "https://www.googleapis.com/urlshortener/v1/url";
$api_url .= "?key=APIキー";
curl_setopt($curl, CURLOPT_URL, $api_url );
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, "{\"longUrl\": \"{$url}\"}" );
$header = array();
$header[] = 'Content-Type: application/json';
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
// $handle = fopen("./header.txt", "w");
// curl_setopt($curl, CURLOPT_WRITEHEADER, $handle);
$result = curl_exec($curl);
curl_close($curl);
return( $result );
}
?>