Created
July 3, 2013 18:42
-
-
Save gvinson/5921525 to your computer and use it in GitHub Desktop.
Post class is for each social network post.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//set timezone (change this to your timezone) | |
date_default_timezone_set("America/Chicago"); | |
class Post { | |
public $text; | |
public $date; | |
public $title; | |
public $link; | |
public function setPost($text) { | |
$this->text = $text; | |
} | |
public function setDate($date) { | |
$this->date = $date; | |
} | |
public function setTitle($title) { | |
$this->title = $title; | |
} | |
public function setLink($link) { | |
$this->link = $link; | |
} | |
public function getLink() { | |
return $this->link; | |
} | |
public function getTitle() { | |
return $this->title; | |
} | |
public function getPost() { | |
return $this->text; | |
} | |
public function getDate() { | |
return $this->date; | |
} | |
} | |
/**---------------------------------------------- | |
* | |
* Facebook Section | |
* | |
*----------------------------------------------*/ | |
//function to retrieve posts from facebook server | |
function loadFB($fbID){ | |
//facebook feed url | |
$url="http://www.facebook.com/feeds/page.php?id=".$fbID."&format=atom10"; | |
//load and setup CURL | |
$c = curl_init(); | |
//set options and make it up to look like firefox | |
$userAgent = "Firefox (WindowsXP) - Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"; | |
curl_setopt($c, CURLOPT_USERAGENT, $userAgent); | |
curl_setopt($c, CURLOPT_URL,$url); | |
curl_setopt($c, CURLOPT_FAILONERROR, true); | |
curl_setopt($c, CURLOPT_FOLLOWLOCATION, true); | |
curl_setopt($c, CURLOPT_AUTOREFERER, true); | |
curl_setopt($c, CURLOPT_RETURNTRANSFER,true); | |
curl_setopt($c, CURLOPT_VERBOSE, false); | |
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); | |
//get data from facebook and decode XML | |
$page = curl_exec($c); | |
$pxml= new SimpleXMLElement($page); | |
//close the connection | |
curl_close($c); | |
//return the data as an object | |
return $pxml->entry; | |
} | |
/**---------------------------------------------- | |
* | |
* Twitter Section | |
* | |
*----------------------------------------------*/ | |
class TwitterTimeline { | |
public $count; | |
public $username; | |
private $bearer_token; | |
private $consumer_key; | |
private $consumer_secret; | |
public function __construct($consumer_key,$consumer_secret) | |
{ | |
$this->consumer_key = $consumer_key; | |
$this->consumer_secret = $consumer_secret; | |
$this->setCount(5); | |
} | |
public function setUsername($username) | |
{ | |
$this->username = $username; | |
} | |
public function setCount($count) | |
{ | |
if(is_int($count)) | |
{ | |
$this->count = $count; | |
} | |
} | |
/** | |
* Get the Bearer Token, this is an implementation of steps 1&2 | |
* from https://dev.twitter.com/docs/auth/application-only-auth | |
*/ | |
function getBearerToken(){ | |
// Step 1 | |
// step 1.1 - url encode the consumer_key and consumer_secret in accordance with RFC 1738 | |
$encoded_consumer_key = urlencode($this->consumer_key); | |
$encoded_consumer_secret = urlencode($this->consumer_secret); | |
// step 1.2 - concatinate encoded consumer, a colon character and the encoded consumer secret | |
$this->bearer_token = $encoded_consumer_key.':'.$encoded_consumer_secret; | |
// step 1.3 - base64-encode bearer token | |
$base64_encoded_bearer_token = base64_encode($this->bearer_token); | |
// step 2 | |
$url = "https://api.twitter.com/oauth2/token"; // url to send data to for authentication | |
$headers = array( | |
"POST /oauth2/token HTTP/1.1", | |
"Host: api.twitter.com", | |
"User-Agent: jonhurlock Twitter Application-only OAuth App v.1", | |
"Authorization: Basic ".$base64_encoded_bearer_token."", | |
"Content-Type: application/x-www-form-urlencoded;charset=UTF-8", | |
"Content-Length: 29" | |
); | |
$ch = curl_init(); // setup a curl | |
curl_setopt($ch, CURLOPT_URL,$url); // set url to send to | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // set custom headers | |
curl_setopt($ch, CURLOPT_POST, 1); // send as post | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return output | |
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials"); // post body/fields to be sent | |
$header = curl_setopt($ch, CURLOPT_HEADER, 1); // send custom headers | |
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
$response = curl_exec ($ch); // execute the curl | |
if ($response === FALSE) { | |
die("Curl failed: " . curl_error($ch)); | |
} | |
curl_close($ch); // close the curl | |
$output = explode("\n", $response); | |
$bearer_token = ''; | |
foreach($output as $line) | |
{ | |
if($line === false) | |
{ | |
// there was no bearer token | |
}else{ | |
$bearer_token = $line; | |
} | |
} | |
$bearer_token = json_decode($bearer_token); | |
$this->bearer_token = $bearer_token->{'access_token'}; | |
} | |
/** | |
* Invalidates the Bearer Token | |
* Should the bearer token become compromised or need to be invalidated for any reason, | |
* call this method/function. | |
*/ | |
function invalidateBearerToken(){ | |
$encoded_consumer_key = urlencode($this->consumer_key); | |
$encoded_consumer_secret = urlencode($this->consumer_secret); | |
$consumer_token = $encoded_consumer_key.':'.$encoded_consumer_secret; | |
$base64_encoded_consumer_token = base64_encode($consumer_token); | |
// step 2 | |
$url = "https://api.twitter.com/oauth2/invalidate_token"; // url to send data to for authentication | |
$headers = array( | |
"POST /oauth2/invalidate_token HTTP/1.1", | |
"Host: api.twitter.com", | |
"User-Agent: jonhurlock Twitter Application-only OAuth App v.1", | |
"Authorization: Basic ".$base64_encoded_consumer_token."", | |
"Accept: */*", | |
"Content-Type: application/x-www-form-urlencoded", | |
"Content-Length: ".(strlen($this->bearer_token)+13)."" | |
); | |
$ch = curl_init(); // setup a curl | |
curl_setopt($ch, CURLOPT_URL,$url); // set url to send to | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // set custom headers | |
curl_setopt($ch, CURLOPT_POST, 1); // send as post | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return output | |
curl_setopt($ch, CURLOPT_POSTFIELDS, "access_token=".$this->bearer_token.""); // post body/fields to be sent | |
$header = curl_setopt($ch, CURLOPT_HEADER, 1); // send custom headers | |
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
$response = curl_exec ($ch); // execute the curl | |
if (curl_exec($ch) === FALSE) { | |
die("Curl failed: " . curl_error($ch)); | |
} | |
curl_close($ch); // close the curl | |
return $response; | |
} | |
function getUserTimeline(){ | |
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json"; // base url | |
$q = urlencode(trim($this->username)); // username term | |
$formed_url ='?screen_name='.$q; // fully formed url | |
$formed_url = $formed_url.'&count='.$this->count; // results per page - defaulted to 15 | |
$headers = array( | |
"GET /statuses/user_timeline.json".$formed_url." HTTP/1.1", | |
"Host: api.twitter.com", | |
"User-Agent: jonhurlock Twitter Application-only OAuth App v.1", | |
"Authorization: Bearer ".$this->bearer_token."", | |
); | |
$ch = curl_init(); // setup a curl | |
curl_setopt($ch, CURLOPT_URL,$url.$formed_url); // set url to send to | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // set custom headers | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return output | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
$response = curl_exec ($ch); // execute the curl | |
if ($response === FALSE) { | |
die("Curl failed: " . curl_error($ch)); | |
} | |
curl_close($ch); // close the curl | |
$this->invalidateBearerToken(); | |
return $response; | |
} | |
public function get() { | |
$this->getBearerToken(); | |
return $this->getUserTimeline(); | |
} | |
// Create Post obj with Twitter feed | |
// | |
////////////////////////////////////// | |
public function createPosts($Post2, $Post4) { | |
$this->getBearerToken(); | |
$json = json_decode($this->getUserTimeline(), FALSE); | |
$Post2->setPost($json[0]->text); | |
$Post2->setDate(strtotime($json[0]->created_at)); | |
$Post4->setPost($json[1]->text); | |
$Post4->setDate(strtotime($json[1]->created_at)); | |
} | |
} | |
// Init Posts objects | |
// | |
////////////////////////////////////////// | |
$Post1 = new Post(); | |
$Post2 = new Post(); | |
$Post3 = new Post(); | |
$Post4 = new Post(); | |
// Facebook Calls | |
// | |
////////////////////////////////////////// | |
//BE SURE to enter your facebook id here | |
$fbid="424084494334939"; | |
//how may posts to show | |
$fbLimit=2; | |
//variable used to count how many weÕve loaded | |
$fbCount=0; | |
//call the function and get the posts from facebook | |
$myPosts=loadFB($fbid); | |
//loop through all the posts we got from facebook | |
foreach($myPosts as $dPost) { | |
//get the post date / time and convert to unix time | |
$dTime = strtotime($dPost->published); | |
//set the post title | |
$postTitle = $dPost->title; | |
//get the entire post text | |
$postText = $dPost->content; | |
$postText = explode("<br/>",$postText); | |
$postText = $postText[0]; | |
//get the link to the post | |
$postLink = $dPost->link->attributes()->href; | |
$fbCount++; | |
if($fbCount == 1) { | |
$Post1->setPost($postText); | |
$Post1->setDate($dTime); | |
$Post1->setTitle($postTitle); | |
$Post1->setLink($postLink); | |
} else { | |
$Post3->setPost($postText); | |
$Post3->setDate($dTime); | |
$Post3->setTitle($postTitle); | |
$Post3->setLink($postLink); | |
} | |
//if we've outputted the number set above in fblimit we're done | |
if($fbCount >= $fbLimit) break; | |
} | |
// Twitter Calls | |
// | |
////////////////////////////////////////// | |
$timeline = new TwitterTimeline('bwBCoUztWYSGB5VjYwA','FUHCKOXLHoeW3ocOPtWO3lyCDiDhZPDZ0Fbqk2Z8Js'); | |
$timeline->setUsername('TBGSearch'); | |
$timeline->setCount(2); | |
$timeline->createPosts($Post2, $Post4); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment