【问题标题】:PHP posting to twitter in bitly url formatPHP以bitly url格式发布到twitter
【发布时间】:2012-09-05 15:23:43
【问题描述】:

我已经设法编写了以下脚本:

<?php

/* make a URL small */
function make_bitly_url($url,$login,$appkey,$format = 'xml',$version = '2.0.1')
{
    //create the URL
    $bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$appkey.'&format='.$format;

    //get the url
    //could also use cURL here
    $response = file_get_contents($bitly);

    //parse depending on desired format
    if(strtolower($format) == 'json')
    {
        $json = @json_decode($response,true);
        return $json['results'][$url]['shortUrl'];
    }
    else //xml
    {
        $xml = simplexml_load_string($response);
        return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
    }
}


//function to get the url of the event! 
function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}

/* usage */
$short = make_bitly_url('http://site.com/viewEvent.php?id=2323232','bitlyuser','bitlyapikey','json');
echo 'The short URL is:  '.$short . "<br>"; 
echo "PATH: ". curPageURL();
// returns:  http://bit.ly/11Owun
?>

现在这段代码可以生成一个简短的 url 传递给它的任何内容。我的网站上有一个来自twitter developer site 的推文按钮。它的工作原理是它发布它当前所在页面的完整链接......所以不是缩短版本。现在我想在按下该推特按钮时生成一个短网址,以便我可以在我的网站帐户上分享。这是怎么做到的?

谢谢,

【问题讨论】:

    标签: php json twitter


    【解决方案1】:

    您应该能够将data-url 选项设置为bitly url。例如

    <a href="https://twitter.com/share" class="twitter-share-button" data-url="<?php echo make_bitly_url('http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"],LOGIN,APPKEY); ?>">Tweet</a>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-14
      • 1970-01-01
      • 2011-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多