BLOG ENTRY

ページの短縮URLを貼り付けたTwitter投稿をしてもらう

twitter

閲覧ユーザーがその場で、ページの短縮URLを貼り付けたTwitter投稿をしてもらう機能をPHPで。

Twitterへの投稿方法

書式はこれ
http://twitter.com/home?status=MESSAGE
で、MESSAGEの部分にデフォルト挿入しておきたい文字列をいれる
★ここをクリックしてみる★
UTF-8エンコード
URLエンコード
はたぶん必須。
で、@や#を入れたい場合は前後を半角スペース。

で、URLを短くしたい場合

http://bit.ly/pages/tools/developer-tools/
(Twitter標準採用しているURL短縮の外部サービス)
アカウント作成すれば
ログインIDとAPIキーを発行してくれる

PHPで書いてみる

window.open先PHPファイル
<?php

//bit.ryのアカウント
$login      = "ログインID";
$apikey     = "APIキー";
$apiversion = "2.0.1";
$format     = "json";

$bitly = new Bitly($login, $apikey, $apiversion, $format); //class内容は後述
$short_url = $bitly->shorten($this_url);
$message = rawurlencode('@★★' . ' ' . '文字列' . ' ' . $short_url . ' #' . 'タグ検索用文字列'); //一例
header("Location: http://twitter.com/home?status={$message}");

で、Bitlyクラス

<?php
/*
* 外部サービスbit.lyを使ってURLを短縮するクラス(PHP4、5共通)
*/
include_once('JSON.php');   //JSONデコード用(まぁなんでもいいです)
class Bitly
{
    var $BITLY_API_URL = 'http://api.bit.ly';
    var $login;
    var $apikey;
    var $apiversion;
    var $format        = 'json';

    function Bitly($login = null, $apikey = null, $apiversion = null, $format = null)
    {
        if ($login !== null) {
            $this->setLogin($login);
        }

        if ($apikey !== null) {
            $this->setApikey($apikey);
        }

        if ($apiversion !== null) {
            $this->setApiVersion($apiversion);
        }

        if ($format !== null) {
            $this->setFormat($format);
        }
    }

    function shorten($longurl)
    {
        $apiurl = $this->BITLY_API_URL   . '/shorten?'
                                        . 'version='    . $this->apiversion
                                        . '&longUrl='   . urlencode($longurl)
                                        . '&login='     . $this->login
                                        . '&apiKey='    . $this->apikey
                                        . '&format='    . $this->format
                                        . '';

        $response = file_get_contents($apiurl);
        switch ($this->format) {
            case 'json' :
                $json = new Services_JSON();
                $data = $json->decode($response);
                $data = $this->conv_obj($data);
                if ($data['errorCode'] === 0 && $data['statusCode'] === 'OK') {
                    $result =  $data['results'][$longurl]['shortUrl'];
                }else{
                    $result =  $data['errorCode'];
                }
                break;
            case 'xml'  :
                $data = $this->_xml_parse($response);
                $result = $this->get_short_url($data);
                break;
            default :
                $result = false;
                break;
        }
        return $result;
    }

    function errors($errorcode)
    {
        $apiurl = $this->BITLY_API_URL  . '/errors?'
                                        . 'version='    . $this->apiversion
                                        . '&login='     . $this->login
                                        . '&apiKey='    . $this->apikey
                                        . '&format='    . $this->format
                                        . '';
        $response = file_get_contents($apiurl);
        switch ($this->format) {
            case 'json' :
                $json = new Services_JSON();
                $data = $json->decode($response);
                $data = $this->conv_obj($data);
                if ($data['errorCode'] === 0 && $data['statusCode'] === 'OK') {
                    $result =  $data['results'][$longurl]['shortUrl'];
                }else{
                    $result =  $data['errorCode'];
                }
                break;
            case 'xml'  :
                $data = $this->_xml_parse($response);
                $result = $this->get_short_url($data);
                break;
            default :
                $result = false;
                break;
        }
        return $result;
    }

    function _xml_parse ($xml_data)
    {
        $xp = xml_parser_create();
        $index = null;
        xml_parse_into_struct($xp, $xml_data, $result, $index);
        xml_parser_free($xp);
        return $result;
    }

    function get_short_url ($data = array())
    {
        foreach ($data as $k => $v) {
            if (is_array($v) && $v['tag'] === 'SHORTURL') {
                return $v['value'];
            } else {
                continue;
            }
        }
    }

    function setLogin($login)
    {
        $this->login = (string) $login;
    }
    function setApikey($apikey)
    {
        $this->apikey = (string) $apikey;
    }

    function setApiVersion($apiversion)
    {
        $this->apiversion = (string) $apiversion;
    }
    function setFormat($format)
    {
        $this->format = (string) $format;
    }

    function conv_obj($data){
        if (!is_object($data) && !is_array($data)) {
            return $data;
        }

        if(is_object($data)){
            foreach(get_object_vars($data) as $key => $val){
                    $ret[$key]=$this->conv_obj($val);
            }
            return $ret;
        }else{
            foreach($data as $key => $val){
                    $ret[$key]=$this->conv_obj($val);
            }
            return $ret;
        }
    }
}

週末は何しようかな。ゲームでもしよう。

No related posts.

WRITE COMMENT


(required)


(required)


(required)

MENU

veltica creative of twitter