使用Google API创建快捷链接
我需要一个简短的链接,所以我把它做成了php。
注册Google API后,您可以使用它。
1 | 使用方法:shortur_google('原始地址','api键'); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <?php function shorturl_google( $url , $key ) { $post = array ( 'longUrl' => $url ); $json = json_encode( $post ); $curlObj = curl_init(); curl_setopt( $curlObj , CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url?key=' . $key ); curl_setopt( $curlObj , CURLOPT_RETURNTRANSFER, 1); curl_setopt( $curlObj , CURLOPT_SSL_VERIFYPEER, 0); curl_setopt( $curlObj , CURLOPT_HEADER, 0); curl_setopt( $curlObj , CURLOPT_HTTPHEADER, array ( 'Content-type:application/json' )); curl_setopt( $curlObj , CURLOPT_POST, 1); curl_setopt( $curlObj , CURLOPT_POSTFIELDS, $json ); $json = json_decode(curl_exec( $curlObj )); curl_close( $curlObj ); return $json ->id; } echo shorturl_google( 'http://kalmuri.kilho.net/' , 'API-KEY-API-KEY-API-KEY' ); ?> |
Leave a Reply