PHP MODx片段:推文
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP MODx片段:推文相关的知识,希望对你有一定的参考价值。
<?php
/**
* @name TweetThis
* @author Dave Child <dave@addedbytes.com>
* @license GPLv3
* @version 0.3
*
* This snippet returns a link to twitter for the user to tweet the current
* page. URL is sent through TinyURL automatically (TinyURLs are cached
* indefinitely in assets/cache/tweetthis.txt). Link is given the class
* "tweetthis".
*
* &name=`@DaveChild`
* Pass through a name to the snippet. You can use your own Twitter ID by
* prefixing the name with an @. Optional.
*
* &text=`Tweet This!`
* Text to use for the link. Optional.
*
* &id=`7`
* Document ID (defaults to current document). Optional.
*/
// ---------------------------------------------------
// Check input variables
// ---------------------------------------------------
$name = (isset($name)) ? $name : '';
$text = (isset($text)) ? $text : 'Tweet This!';
$id = (isset($id)) ? $id : $modx->documentIdentifier;
// ---------------------------------------------------
// Get document information
// ---------------------------------------------------
$thisDoc = $modx->getPageInfo($id);
// ---------------------------------------------------
// Define getTinyUrl function
// ---------------------------------------------------
if (!function_exists('getTinyUrl')) {
function getTinyUrl($id) {
global $modx;
$cacheFile = $modx->config['base_path'] . 'assets/cache/tweetthis.txt';
if (file_exists($cacheFile)) {
$tinyurls = unserialize(file_get_contents($cacheFile));
if ($tinyurls[$id]) {
return $tinyurls[$id];
}
} else {
$tinyurls = array();
}
$url = $modx->makeUrl($id, '', '', 'full');
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, 'http://tinyurl.com/api-create.php?url=' . $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
$tinyurl = curl_exec($ch);
curl_close($ch);
if ($tinyurl) {
$tinyurls[$id] = $tinyurl;
} else {
return $url;
}
$fp = fopen($cacheFile, 'w');
fwrite($fp, serialize($tinyurls));
fclose($fp);
return $tinyurl;
}
}
// ---------------------------------------------------
// Build link
// ---------------------------------------------------
$strTweetThis = '<a class="tweetthis" href="http://twitter.com/home?status=' . urlencode($thisDoc['pagetitle']);
if ($name <> '') {
$strTweetThis .= '%20by%20' . urlencode($name);
}
$strTweetThis .= ':%20' . urlencode(getTinyUrl($id)) .'">' . $text . '</a>';
return $strTweetThis;
?>
以上是关于PHP MODx片段:推文的主要内容,如果未能解决你的问题,请参考以下文章