我正在使用Twitter API 1.1制作一个wep应用程序,并成功创建了一些功能,可以引入指定的用户个人资料图像,实际名称和最新推文。全部使用OAuth对请求进行身份验证。
我的问题是 - 所有创建的3个函数都需要定义一个twitter用户名。目前,我必须为我输入的每个Twitter用户名一遍又一遍地复制我的代码。当然有更好的方法吗?目前我的代码看起来并不好看。如果有人可以提供帮助,我会非常感激。我对PHP很陌生,所以我不知所措。
请参阅下面的代码: 功能:
function getTweets($user, $tmhOAuth){
$usertweets = array();
$code = $tmhOAuth->request('GET', $tmhOAuth->url('1.1/statuses/user_timeline'), array(
'include_entities' => '1',
'include_rts' => '1',
'screen_name' => $user,
'count' => 1,
'exclude_replies' => 'true',
'contributor_details' => 'true'
));
if ($code == 200) {
$timeline = json_decode($tmhOAuth->response['response'], true);
foreach ($timeline as $tweet) :
$entified_tweet = tmhUtilities::entify_with_options($tweet);
$is_retweet = isset($tweet['retweeted_status']);
$diff = time() - strtotime($tweet['created_at']);
if ($diff < 60*60)
$created_at = floor($diff/60) . ' minutes ago';
elseif ($diff < 60*60*24)
$created_at = floor($diff/(60*60)) . ' hours ago';
else
$created_at = date('d M', strtotime($tweet['created_at']));
$permalink = str_replace(
array(
'%screen_name%',
'%id%',
'%created_at%'
),
array(
$tweet['user']['screen_name'],
$tweet['id_str'],
$created_at,
),
'<a href="https://twitter.com/%screen_name%/%id%">%created_at%</a>'
);
$tweet['created_at'] = $created_at;
$usertweets[] = $tweet;
endforeach;
} else {
tmhUtilities::pr($tmhOAuth->response);
}
return $usertweets;
}
function getImage($screen_name,$tmhOAuth,$size = ''){
$url = $tmhOAuth->request('GET', $tmhOAuth->url('1.1/users/show'), array(
'screen_name' => $screen_name,
));
$results = json_decode($tmhOAuth->response['response'], true);
//Get the user's profile image
$profileImg = ($results['profile_image_url']);
return str_replace('_normal', $size, $profileImg);
}
function getName($screen_name,$tmhOAuth){
$url = $tmhOAuth->request('GET', $tmhOAuth->url('1.1/users/show'), array(
'screen_name' => $screen_name,
));
$results = json_decode($tmhOAuth->response['response'], true);
//Get the user's name
$name = ($results['name']);
return $name;
}
如何实施这些功能:
$name2 = getName("JoeyEssex_",$tmhOAuth);
$user2 = getTweets("JoeyEssex_",$tmhOAuth);
$image2 = getImage("JoeyEssex_",$tmhOAuth);
$name3 = getName("piersmorgan",$tmhOAuth);
$user3 = getTweets("piersmorgan",$tmhOAuth);
$image3 = getImage("piersmorgan",$tmhOAuth);
$name4 = getName("BinkyFelstead",$tmhOAuth);
$user4 = getTweets("BinkyFelstead",$tmhOAuth);
$image4 = getImage("BinkyFelstead",$tmhOAuth);
//Display the profile images of specified users
echo '<img src="' . $image2 . '" width="240px" height="240px" />';
echo '<img src="' . $image3 . '" width="240px" height="240px" />';
echo '<img src="' . $image4 . '" width="240px" height="240px" />';
//Display user's name
echo '<p>Latest tweet from <b>' . $name1 . '</b>:<br />';
//Display their tweets
foreach($user1 as $tweet){
echo $tweet['text'] . '<br />';
echo "Sent: <b>" . $tweet['created_at'] . "</b></p>";
}
echo '<b>' . $name2 . '</b> ';
foreach($user2 as $tweet){
echo $tweet['text'];
echo " " . $tweet['created_at'] . "<br />";
}
echo '<b>' . $name3 . '</b> ';
foreach($user3 as $tweet){
echo $tweet['text'];
echo " " . $tweet['created_at'] . "<br />";
}
echo '<b>' . $name4 . '</b> ';
foreach($user4 as $tweet){
echo $tweet['text'];
echo " " . $tweet['created_at'] . "<br />";
}
你看到可怕的重复?必须有更好的方法!
答案 0 :(得分:0)
重复代码中唯一不同的部分实际上是推文人员的姓名。因此,创建一个包含所有推特名称的列表并迭代它。更改foreach
循环内的代码以反映新的输入和处理:
$tweeters = array('JoeyEssex_', 'piersmorgan', 'BinkyFelstead');
foreach ($tweeters as $tweeter)
{
$tweeter_name = getName($tweeter, $tmhOAuth);
$tweeter_tweets = getTweets($tweeter, $tmhOAuth);
$tweeter_image = getImage($tweeter, $tmhOAuth);
// Display the profile images of user
echo '<img src="' . $tweeter_image . '" width="240px" height="240px" />';
// Display user's name
echo '<p>Latest tweet from <b>' . tweeter_name . '</b>:<br />';
// Display user's tweets
foreach ($tweeter_tweets as $tweet)
{
echo $tweet['text'] . '<br />';
echo "Sent: <b>" . $tweet['created_at'] . "</b></p>";
}
}
将重复放入循环是一种表达代码重复的常用方法,意思是一遍又一遍地执行相同操作。
此外,您甚至可以开始将功能包装到Tweeter
类中:
class Tweeter {
private $name;
private $tmhOAuth;
public function __construct($name, $tmhOAuth) {
$this->name = $name;
$this->tmhOAuth = $tmhOAuth;
}
public function getName() {
return getName($this->name, $this->tmhOAuth);
}
public function getTweets() {
return getTweets($this->name, $this->tmhOAuth);
}
public function getImage() {
return getImage($this->name, $this->tmhOAuth);
}
}
这是快速编写的,当然您甚至可以将代码从现有的get...
函数移动到类方法中,但我会一步一步地进行快速的第一次更改无需删除现有代码。
然后用法非常简单:
$tweeters = array('JoeyEssex_', 'piersmorgan', 'BinkyFelstead');
// Create all Tweeter objects
foreach ($tweeters as $i => $tweeter)
{
$tweeters[$i] = new Tweeter($tweeter, $tmhOAuth);
}
// Display all Tweeters
foreach ($tweeters as $tweeter)
{
echo '<img src="' . $tweeter->getImage() . '" width="240px" height="240px" />';
echo '<p>Latest tweet from <b>' . $tweeter->getName() . '</b>:<br />';
foreach ($tweeter->getTweets() as $tweet)
{
echo $tweet['text'] . '<br />';
echo "Sent: <b>" . $tweet['created_at'] . "</b></p>";
}
}
这将使您能够从面向对象的编程开始获利。