redis hashtag

tech2023-08-09  118

redis hashtag

介绍 (Introduction)

Social networking sites play a vital role in increasing the popularity of a website or application. The vast majority of web sites provide the ability to share their content on popular social media sites with the click of a button. Organizing a contest on these sites is another excellent way of promoting your application.

社交网站在增加网站或应用程序的流行度方面起着至关重要的作用。 绝大多数网站都提供了通过单击按钮在流行的社交媒体网站上共享其内容的功能。 在这些站点上组织竞赛是推广您的应用程序的另一种极好的方法。

We are going to build a contest on Twitter to enhance user interaction with a web application. This will be a two part series where the first part focuses on introducing you to Twitter contests and configuring a Twitter application, while the second part will focus on the implementation of the contest. Let’s get started.

我们将在Twitter上举办竞赛,以增强用户与Web应用程序的互动。 这将是一个分为两部分的系列,其中第一部分着重于向您介绍Twitter竞赛和配置Twitter应用程序,而第二部分则着重于竞赛的实施。 让我们开始吧。

Twitter竞赛简介 (Introduction to Contests in Twitter)

Twitter is a social networking and micro blogging service utilizing instant messaging, SMS or a web interface. Twitter is considered to be the SMS of web, so contests on Twitter will usually be different from contests on other popular social networking sites.

Twitter是利用即时消息,SMS或Web界面的社交网络和微博客服务。 Twitter被认为是Web的SMS,因此Twitter上的比赛通常与其他流行的社交网站上的比赛有所不同。

The main goal of a Twitter contest is to get people to create as many tweets as possible about a certain topic to improve awareness. Let’s take a look at some of the most popular types of contests using Twitter.

Twitter竞赛的主要目的是使人们就特定主题创建尽可能多的推文,以提高知名度。 让我们来看看使用Twitter的一些最受欢迎的比赛类型。

Sweepstakes – This is the simplest type of Twitter contests. Users can enter the contest by creating a tweet with a given #tag and their username. The winner will be picked through a lucky draw.

抽奖活动 -这是最简单的Twitter竞赛类型。 用户可以通过创建带有给定#tag和用户名的推文来参加比赛。 幸运抽奖将选出优胜者。

Creative Answer – In this contest, organizers ask a question by creating a tweet or publishing the question on their website. User have to create tweets with a given #tag to answer the question. The most creative answer will be chosen manually by the organizers.

创意答案 –在此竞赛中,组织者通过创建推文或在网站上发布问题来提出问题。 用户必须使用给定的#tag创建推文来回答问题。 最具创意的答案将由组织者手动选择。

Retweet to win – Organizers create a tweet and users have to retweet it. The winner will be picked randomly from all the retweeters.

转推以赢得胜利 –组织者创建了一条推文,用户必须转推。 将从所有转发者中随机选出获胜者。

Follow to win – Organizers provide a company profile or the profile they want to promote. Users have to be a follower of given profile. Picking the winners is usually done randomly at the end of the contest.

追求成功 –组织者提供公司简介或他们要宣传的简介。 用户必须是给定个人资料的关注者。 通常在比赛结束时随机选择获奖者。

Instead of relying entirely on luck, we’ll create a hashtag contest that generates a winner based on the effort and performance of the users.

而不是完全依靠运气,我们将创建一个标签竞赛,该竞赛根据用户的努力和表现产生赢家。

计划Twitter标签竞赛 (Planning a Twitter Hashtag Contest)

Here are the steps of the contest:

以下是比赛的步骤:

Users need to authenticate their Twitter account – Usually, this is done by providing a Twitter login button where users can authorize access to the application through their Twitter account.

用户需要验证其Twitter帐户的身份 -通常,这是通过提供Twitter登录按钮来完成的,用户可以在其中通过其Twitter帐户授权对应用程序的访问。

Create Tweets with Hashtags through our application – We are going to use two hashtags with each tweet. The first tag is used to promote the brand. If our brand is SitePoint, we will have a hashtag called #SitePoint to promote our brand. The second tag is used to identify the application amongst the other tweets with the first hashtag.

通过我们的应用程序使用标签创建推文 –我们将在每个推文中使用两个标签。 第一个标签用于推广品牌。 如果我们的品牌是SitePoint,我们将有一个名为#SitePoint的主题标签来推广我们的品牌。 第二个标签用于在带有第一个#标签的其他推文中标识应用程序。

Select the winners – There are many ways of picking winners automatically in such contests. In this case, we are going to evaluate winners based on the number of retweets. The winner will be picked based on the total number of retweets for the tweets the users create.

选择获奖者 –在此类竞赛中,有很多自动选择获奖者的方法。 在这种情况下,我们将根据转发次数评估获胜者。 将根据用户创建的推文的转发总数选出获胜者。

建造 (Building)

To begin, you need to register an application on Twitter. Visit the Twitter Developer site and create an application with write access. Once you have a Twitter application with consumer_key and consumer_secret, follow the instructions in the step by step guide to complete the implementation.

首先,您需要在Twitter上注册一个应用程序。 访问Twitter开发人员站点并创建具有写访问权的应用程序。 一旦拥有带有consumer_key和consumer_secret的Twitter应用程序,请按照分步指南中的说明完成实现。

第1步–配置OAuth库 (Step 1 – Configuring the OAuth Library)

OAuth is the standard technique used by Twitter to authenticate access to their API. There are many existing libraries for automating the authentication process with Twitter. We are going to use a library called tmhOAuth. Download and extract the zip file from Github, or clone it.

OAuth是Twitter用于验证对其API的访问权限的标准技术。 有许多现有的库可用于通过Twitter自动执行身份验证过程。 我们将使用一个名为tmhOAuth的库。 从Github下载并解压缩该zip文件,或对其进行克隆。

Create the application folder with an index.php file inside it and copy the contents of tmhOAuth library into a folder called twitter. Then include the tmhOAuth.php file inside the index.php file (see below).

创建其中包含index.php文件的应用程序文件夹,并将tmhOAuth库的内容复制到名为twitter的文件夹中。 然后,将tmhOAuth.php文件包含在index.php文件中(请参见下文)。

步骤2 –使用Twitter验证用户 (Step 2 – Authenticating Users with Twitter)

First, users have to authorize the app using their Twitter account. We need a button or link that redirects the user to the authorization process. We are going to use a separate class for managing the logic of this application. Create a file called twitter_hashtag_contest.php inside the application folder. Now let’s take a look at the updated index.php file.

首先,用户必须使用其Twitter帐户授权该应用程序。 我们需要一个按钮或链接来将用户重定向到授权过程。 我们将使用一个单独的类来管理此应用程序的逻辑。 在应用程序文件夹中创建一个名为twitter_hashtag_contest.php的文件。 现在,让我们看一下更新的index.php文件。

<?php require 'twitter/tmhOAuth.php'; require 'twitter_hashtag_contest.php'; session_start(); $contest = new TwitterHashTagContest(); if(isset($_GET['action']) && $_GET['action'] == 'twitter'){ $contest->getRequestToken(); } else { echo "<a href='?action=twitter'>LogIn with Twitter</a>"; }

We can display the login link by default. Once the login link is clicked, we have to redirect the user to Twitter for app authorization and retrieving the request tokens. We have to set up the initial details of TwitterHashTagContest class, before we move into the implementation of getRequestToken function.

我们可以默认显示登录链接。 单击登录链接后,我们必须将用户重定向到Twitter,以进行应用授权并检索请求令牌。 在进入getRequestToken函数的实现之前,我们必须设置TwitterHashTagContest类的初始详细信息。

步骤3 –初始化TwitterHashTagContest类 (Step 3 – Initializing TwitterHashTagContest Class)

We are using TwitterHashTagContest class to handle all the application specific details, so let’s look at the initialization and configuration for said class.

我们正在使用TwitterHashTagContest类来处理所有应用程序特定的详细信息,因此让我们看一下该类的初始化和配置。

class TwitterHashTagContest{ private $config; private $twitter_auth; private $app_details; private $result_tweets; public function __construct(){ $this->config['consumer_key'] = 'consumer_key'; $this->config['consumer_secret'] = 'consumer_secret'; $this->config['OAUTH_CALLBACK']='URL to index.php file'; $this->twitter_auth = new tmhOAuth($this->config); $this->result_tweets = array(); } }

We can start the configuration by defining consumer_key,consumer_secret and callback URL in an array. Then we can initialize the tmhOAuth library using the configuration array. Finally, we initialize an array for storing the results at the completion of the contest.

我们可以通过在数组中定义consumer_key , consumer_secret和回调URL来开始配置。 然后,我们可以使用配置数组初始化tmhOAuth库。 最后,我们在比赛结束时初始化一个数组,用于存储结果。

Having completed configuration details, now we can move into the Twitter authorization process.

完成配置详细信息后,现在我们可以进入Twitter授权过程。

步骤4 –生成请求令牌 (Step 4 – Generate Request Tokens)

First, we have to get request tokens to initialize the user authorization process. Twitter provides the API URL oauth/request_token for generating request tokens. Let’s take a look at the implementation of the getRequestToken function defined earlier.

首先,我们必须获取请求令牌以初始化用户授权过程。 Twitter提供了用于生成请求令牌的API URL oauth/request_token 。 让我们看一下前面定义的getRequestToken函数的实现。

public function getRequestToken() { $this->twitter_auth->request("POST", $this->twitter_auth->url("oauth/request_token", ""), array( 'oauth_callback' => $this->config['OAUTH_CALLBACK'] )); if ($this->twitter_auth->response["code"] == 200) { // get and store the request token $response = $this->twitter_auth->extract_params($this->twitter_auth->response["response"]); $_SESSION["authtoken"] = $response["oauth_token"]; $_SESSION["authsecret"] = $response["oauth_token_secret"]; // redirect the user to Twitter to authorize $url = $this->twitter_auth->url("oauth/authenticate", "") . '?oauth_token=' . $response["oauth_token"]; header("Location: " . $url); exit; } return false; }

We already have an instance of the tmhOAuth class, initialized within the constructor of TwitterHashTagContest. We have to use the request method of tmhOAuth to access Twitter’s API. This function takes 3 arguments where the first one defines the request type (POST or GET) and the next two arguments define the API endpoint URL and parameters respectively.

我们已经有一个tmhOAuth类的实例,该实例在tmhOAuth的构造函数中TwitterHashTagContest 。 我们必须使用tmhOAuth的请求方法来访问Twitter的API。 此函数有3个参数,第一个参数定义请求类型(POST或GET),后两个参数分别定义API端点URL和参数。

We are using the oauth/request_token API URL to generate the tokens. The response will contain the request tokens as well as the status of the request. Code 200 means a successful completion of the request and we proceed by using the extract_params function to extract all the parameters from the response into an array. Next, we store the oauth_token and oauth_token_secret in the current session.

我们正在使用oauth/request_token API URL生成令牌。 响应将包含请求令牌以及请求状态。 代码200表示请求已成功完成,我们通过使用extract_params函数将响应中的所有参数提取到数组中来进行操作。 接下来,我们将oauth_token和oauth_token_secret存储在当前会话中。

Finally, we redirect the user for authorization using oauth/authenticate or oauth/authorize.

最后,我们使用oauth/authenticate或oauth/authorize重定向用户以进行oauth/authorize 。

We can use either oauth/authenticate or oauth/authorize to handle the authorization process. oauth/authorize requires the user to authorize the application on each request, even if it’s already authorized for the user. oauth/authenticate differentiates from oauth/authorize by avoiding authorization on each request. We have to tick the Allow this application to be used to Sign in with Twitter checkbox on the Settings tab of our Twitter application to allow this.

我们可以使用oauth/authenticate或oauth/authorize来处理授权过程。 oauth/authorize要求用户根据每个请求对应用程序进行授权,即使该应用程序已为该用户授权。 oauth/authenticate通过避免对每个请求进行oauth/authorize来区别于oauth/authorize 。 我们必须在我们的Twitter应用程序的“设置”选项卡上勾选“ 允许使用该应用程序通过Twitter登录”复选框,以允许此操作。

Once redirected, users can log in and authorize the application, then Twitter will automatically redirect the request to the callback URL. Next, we have to generate user specific request credentials (access tokens) by validating the request tokens.

重定向后,用户可以登录并授权应用程序,然后Twitter将自动将请求重定向到回调URL。 接下来,我们必须通过验证请求令牌来生成用户特定的请求凭证(访问令牌)。

步骤5 –生成访问令牌 (Step 5 – Generate Access Tokens)

First, we have to filter the response retrieved after authorizing the app. Let’s look at the updated code.

首先,我们必须过滤对应用授权后检索到的响应。 让我们看一下更新后的代码。

if(isset($_GET['action']) && $_GET['action'] == 'twitter'){ $contest->getRequestToken(); } else if(isset($_GET['oauth_token'])) { $oauth_token = isset($_GET['oauth_token']) ? $_GET['oauth_token'] : ''; $oauth_verifier = isset($_GET['oauth_verifier']) ? $_GET['oauth_verifier'] : ''; $response = $contest->getRequestCredentials($oauth_verifier,$oauth_token); $contest->createUserSession($response); } else { echo "<a href='?action=twitter'>LogIn with Twitter</a>"; }

The response from Twitter contains oauth_token and oauth_verifier as URL parameters. We can filter the request using these parameters and call the getRequestCredentials function to generate the access tokens. The following code illustrates the implementation of getRequestCredentials.

Twitter的响应包含oauth_token和oauth_verifier作为URL参数。 我们可以使用这些参数过滤请求,并调用getRequestCredentials函数生成访问令牌。 以下代码说明了getRequestCredentials的实现。

public function getRequestCredentials($oauth_verifier,$oauth_token){ $this->twitter_auth->request("POST", $this->twitter_auth->url("oauth/access_token", ""), array( // pass a variable to set the callback 'oauth_verifier' => $oauth_verifier, 'oauth_token' => $oauth_token )); $response = array(); if($this->twitter_auth->response["code"] == 200) { $response = $this->twitter_auth->extract_params($this->twitter_auth->response["response"]); } return $response; }

As we did earlier, the POST request is created on $this->twitter_auth object to access oauth/access_token API. Here, we are passing the oauth_verifier and oauth_token retrieved from previous request for generating the access tokens. Finally, we check the response code to match 200 and return the extracted components of the response object.

正如我们之前所做的那样,POST请求是在$this->twitter_auth对象上创建的,以访问oauth/access_token API。 在这里,我们传递从先前请求中获取的oauth_verifier和oauth_token来生成访问令牌。 最后,我们检查响应代码以匹配200,并返回提取的响应对象组件。

Please keep in mind that we are only implementing the success path considering the scope of this tutorial. In a real implementation, we will have to implement the error handling part to filter other response codes as well.

请记住,考虑到本教程的范围,我们仅在实现成功途径。 在实际的实现中,我们将必须实现错误处理部分以过滤其他响应代码。

Afterwards, we can use the access tokens and enable tweeting capabilities for the users. We pass the response to a function called createUserSession, for adding the details to the browser session and initializing the tweeting screen.

之后,我们可以使用访问令牌并为用户启用推功能。 我们将响应传递给一个名为createUserSession的函数,该函数用于将详细信息添加到浏览器会话并初始化推文屏幕。

In a real implementation, we need to keep the access tokens and the details of the logged in user in the database to retrieve the tokens. Here, we are using browser sessions instead to simplify the tutorial.

在实际的实现中,我们需要将访问令牌和已登录用户的详细信息保留在数据库中以检索令牌。 在这里,我们使用浏览器会话来简化教程。

结论 (Conclusion)

Promoting applications through social media contests is an excellent approach. Twitter is one of the easiest platforms to host a contest on, so we started with the goal of building a hashtag contest on Twitter.

通过社交媒体竞赛来推广应用程序是一种极好的方法。 Twitter是举办竞赛的最简单平台之一,因此我们的目标是在Twitter上建立主题标签竞赛。

So far, we implemented the initial steps to create a Twitter application and authenticate users into our system. In the next part, we will be completing the implementation of this contest by creating the tweet capabilities for the users and generating the results.

到目前为止,我们已经执行了创建Twitter应用程序并验证用户身份进入我们系统的初始步骤。 在下一部分中,我们将通过为用户创建鸣叫功能并生成结果来完成竞赛的实施。

Until then, let us know your thoughts about this part!

在此之前,请让我们知道您对此部分的想法!

翻译自: https://www.sitepoint.com/building-twitter-hashtag-contest-register-twitter-app/

redis hashtag

相关资源:jdk-8u281-windows-x64.exe
最新回复(0)