logo logo

The next-generation blog, news, and magazine theme for you to start sharing your stories today!

The Blogzine

Save on Premium Membership

Get the insights report trusted by experts around the globe. Become a Member Today!

View pricing plans

New York, USA (HQ)

750 Sing Sing Rd, Horseheads, NY, 14845

Call: 469-537-2410 (Toll-free)

hello@blogzine.com
Php

Connect Twitter API with OAuth using PHP.

Few days back I had connected labs.9lessons application to twitter API via OAuth (open protocal secure authorization). In this post I want to explain how to store twitter oauth_token and secret value…

avatar
Hung.Pro.VN

Developer Windows


  • 15/05/2016
  • Views
Few days back I had connected labs.9lessons application to twitter API via OAuth (open protocal secure authorization). In this post I want to explain how to store twitter oauth_token and secret values in to database and how to update status with your own web application.




Database
Here storing twitter authorization token keys. MySQL users table columns uid, uname, passcode, oauth_token, oauth_token_secret
CREATE TABLE users
(
uid INT PRIMARY KEY AUTO_INCREMENT,
uname VARCHAR(50) UNIQUE,
passcode VARCHAR(50),
oauth_token VARCHAR(90),
oauth_token_secret VARCHAR(90)
);

How it works at labs.9lessons
Take a look at this video.



confirm.php
Callback URL - return to after successfully authentication?
include 'db.php';
include 'EpiCurl.php';
include 'EpiOAuth.php';
include 'EpiTwitter.php';
include 'secret.php';
$Twitter = new EpiTwitter($consumerKey, $consumerSecret);
if(isset($_GET['oauth_token']) || (isset($_COOKIE['oauth_token']) && isset($_COOKIE['oauth_token_secret'])))
{
// Twitter user accepted access
if( !isset($_COOKIE['oauth_token']) || !isset($_COOKIE['oauth_token_secret']) )
{
// user comes from twitter
$Twitter->setToken($_GET['oauth_token']);
$token = $Twitter->getAccessToken();
setcookie('oauth_token', $token->oauth_token);
setcookie('oauth_token_secret', $token->oauth_token_secret);
$Twitter->setToken($token->oauth_token, $token->oauth_token_secret);
}
else
{
// user switched pages and came back or got here directly, stilled logged in
$Twitter->
setToken($_COOKIE['oauth_token'],$_COOKIE['oauth_token_secret']);
$user= $Twitter->get_accountVerify_credentials();
$oauth_token=$_COOKIE['oauth_token'];
$oauth_token_secret=$_COOKIE['oauth_token_secret'];

// Storing token keys
$sql=mysql_query("update users SET oauth_token='$oauth_token',
oauth_token_secret='$oauth_token_secret' where username='$user_session'");
header('Location: abc.php'); //Redirecting Page
}
echo "Please re-connect once again click here";
}
This below status(tweet) updated with labs.9lessons.info

update.php
Here $message="Your Status update";
include 'db.php';
include 'EpiCurl.php';
include 'EpiOAuth.php';
include 'EpiTwitter.php';
include 'secret.php';
$tw_sql=mysql_query("select oauth_token,oauth_token_secret from users where user='$user_session'");
$row=mysql_fetch_array($tw_sql);

$oauth_token=$row['oauth_token'];
$oauth_token_secret=$row['oauth_token_secret'];

if(strlen($oauth_token)>0 || strlen($oauth_token_secret)>0 )
{
$Twitter = new EpiTwitter($consumerKey, $consumerSecret);
$Twitter->setToken($oauth_token,$oauth_token_secret);
//$message Status update
$status=$Twitter->post_statusesUpdate(array('status' => $message));
$status->response;
}

If any queries just ask me at facebook.com/srinivas.tamada
db.php
PHP database configuration file

Related post


avatar

Hung.Pro.VN

Nhà thiết kế Web
View Articles

Tôi là admin trang Hung.Pro.VN là một người có đam mê với Blogspot, kinh nghiệm 5 năm thiết kế ra hàng trăm mẫu Template blogpsot như" Bán hàng, bất động sản, landing page, tin tức...

Nhận xét

Share this article