-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creating server side script to login in Facebook
Creating script to be able to request in the future the Facebook access token through a complete server-side solution
- Loading branch information
Guilherme Quental
committed
Apr 3, 2013
0 parents
commit 1d8bccd
Showing
5 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
vendor |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
require_once "vendor/autoload.php"; | ||
|
||
use HackThursday\Handler\FacebookHandler; | ||
|
||
if (!isset($argv[1])) { | ||
throw new Exception('You need to pass the cookie file path'); | ||
} | ||
|
||
$facebook = new FacebookHandler($argv[1]); | ||
|
||
$facebook->login('email', 'pass'); | ||
|
||
//example request | ||
$string = $facebook->request('https://facebook.com'); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "HackThursday/Eventeiro", | ||
"description": "Get the events from facebook and generates a json file", | ||
"autoload": { | ||
"psr-0": {"HackThursday": "src/"} | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Guilherme Quental", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"facebook/php-sdk": "dev-master" | ||
} | ||
} | ||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
namespace HackThursday\Entity; | ||
|
||
class FacebookUser | ||
{ | ||
private $username; | ||
private $password; | ||
|
||
public function __construct($username = null, $password = null) | ||
{ | ||
if ($username) { | ||
$this->setUsername($username); | ||
} | ||
|
||
if ($password) { | ||
$this->setPassword($password); | ||
} | ||
} | ||
|
||
public function setUsername($username) | ||
{ | ||
$this->username = $username; | ||
|
||
return $this; | ||
} | ||
|
||
public function setPassword($password) | ||
{ | ||
$this->password = $password; | ||
|
||
return $this; | ||
} | ||
|
||
public function getUsername() | ||
{ | ||
return $this->username; | ||
} | ||
|
||
public function getPassword() | ||
{ | ||
return $this->password; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
namespace HackThursday\Handler; | ||
|
||
use HackThursday\Entity\FacebookUser; | ||
|
||
class FacebookHandler | ||
{ | ||
private $cookiePath; | ||
private $user; | ||
|
||
public function __construct($cookiePath) | ||
{ | ||
$this->cookiePath = $cookiePath; | ||
} | ||
|
||
public function login($username, $password) | ||
{ | ||
$this->user = new FacebookUser($username, $password); | ||
|
||
$this->retrieveCookiesAndIdentifiers(); | ||
|
||
$this->facebookLogin(); | ||
} | ||
|
||
public function request($url) | ||
{ | ||
return $this->facebookRequest($url); | ||
} | ||
|
||
private function retrieveCookiesAndIdentifiers() | ||
{ | ||
//credits to Sony AK Knowledge Center - www.sony-ak.com | ||
$curl = curl_init(); | ||
curl_setopt($curl, CURLOPT_URL, "http://www.facebook.com"); | ||
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); | ||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | ||
curl_setopt($curl, CURLOPT_ENCODING, ""); | ||
curl_setopt($curl, CURLOPT_COOKIEJAR, $this->cookiePath); | ||
curl_setopt( | ||
$curl, | ||
CURLOPT_USERAGENT, | ||
"Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) | ||
Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)" | ||
); | ||
$curlData = curl_exec($curl); | ||
curl_close($curl); | ||
|
||
$this->identifiers = $this->retrieveIdentifiers($curlData); | ||
} | ||
|
||
private function retrieveIdentifiers($curlData) | ||
{ | ||
//credits to Sony AK Knowledge Center - www.sony-ak.com | ||
$charsetTest = substr($curlData, strpos($curlData, "name=\"charset_test\"")); | ||
$charsetTest = substr($charsetTest, strpos($charsetTest, "value=") + 7); | ||
$charsetTest = substr($charsetTest, 0, strpos($charsetTest, "\"")); | ||
|
||
$locale = substr($curlData, strpos($curlData, "name=\"locale\"")); | ||
$locale = substr($locale, strpos($locale, "value=" ) + 7); | ||
$locale = substr($locale, 0, strpos($locale, "\"")); | ||
|
||
$lsd = substr($curlData, strpos($curlData, "name=\"locale\"")); | ||
$lsd = substr($lsd, strpos($lsd, "value=") + 7); | ||
$lsd = substr( $lsd, 0, strpos($lsd, "\"")); | ||
|
||
return array($charsetTest, $locale, $lsd); | ||
} | ||
|
||
private function facebookLogin() | ||
{ | ||
return $this->facebookRequest( | ||
"https://login.facebook.com/login.php?login_attempt=1" | ||
); | ||
} | ||
|
||
private function facebookRequest($url) | ||
{ | ||
//credits to Sony AK Knowledge Center - www.sony-ak.com | ||
$curl = curl_init(); | ||
curl_setopt($curl, CURLOPT_URL, $url); | ||
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); | ||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | ||
curl_setopt($curl, CURLOPT_POST, 1); | ||
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); | ||
|
||
list($charsetTest, $locale, $lsd) = $this->identifiers; | ||
|
||
curl_setopt( | ||
$curl, | ||
CURLOPT_POSTFIELDS, | ||
"charset_test=" . $charsetTest . "&locale=" . $locale . | ||
"&non_com_login=&email=" . $this->user->getUsername() . | ||
"&pass=" . $this->user->getPassword() . | ||
"&charset_test=" . $charsetTest . "&lsd=" . $lsd | ||
); | ||
|
||
curl_setopt($curl, CURLOPT_ENCODING, "" ); | ||
curl_setopt($curl, CURLOPT_COOKIEFILE, $this->cookiePath); | ||
curl_setopt($curl, CURLOPT_COOKIEJAR, $this->cookiePath); | ||
curl_setopt( | ||
$curl, | ||
CURLOPT_USERAGENT, | ||
"Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) | ||
Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)" | ||
); | ||
$curlData = curl_exec ( $curl ); | ||
|
||
return $curlData; | ||
} | ||
|
||
} |