Sunday, August 18, 2013

Twitter Hashtag Php Search Simple example



1. Create a app at dev.twitter.com/apps
2. php code below also download the library should work as it is

require_once('TwitterAPIExchange.php');



/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "get from dev.twitter.com/apps/your apps settings",
'oauth_access_token_secret' => "get from dev.twitter.com/apps/your apps settings",
'consumer_key' => "get from dev.twitter.com/apps/your apps settings",
'consumer_secret' => "get from dev.twitter.com/apps/your apps settings"
);

$twitter = new TwitterAPIExchange($settings);


$url = 'https://api.twitter.com/1.1/search/tweets.json';
$requestMethod = 'GET';
$getfield = '?q=#HarshalTestingTwitter&result_type=recent';

// Perform the request
$twitter = new TwitterAPIExchange($settings);
print_r( $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest());

Login /Connect Php Twitter Very simple example


<?php


1. Create a app at
https://dev.twitter.com/apps

2. In Settings set - Allow Signin

3. Download the library from

https://github.com/abraham/twitteroauth

4. Php code below

session_start();
require_once('twitteroauth/twitteroauth.php');


// Set consumerkey and secret
$twitteroauth = new TwitterOAuth('consumerkey from app step 2', 'consumerSecret from app settings step 2');
// Requesting authentication tokens, the parameter is the URL we will be redirected to

// Requesting authentication tokens, the parameter is the URL we will be redirected to
$request_token = $twitteroauth->getRequestToken();

// Saving them into the session
$_SESSION['oauth_token'] = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];

// If everything goes well..
if($twitteroauth->http_code==200){
    // Let's generate the URL and redirect
    $url = $twitteroauth->getAuthorizeURL($request_token['oauth_token']);
    header('Location: '. $url);
} else {
    // It's a bad idea to kill the script, but we've got to know when there's an error.
    die('Something wrong happened.');
}
?>