Skip to content

Commit

Permalink
Creating server side script to login in Facebook
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor
15 changes: 15 additions & 0 deletions bootstrap.php
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');
18 changes: 18 additions & 0 deletions composer.json
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"
}
}


43 changes: 43 additions & 0 deletions src/HackThursday/Entity/FacebookUser.php
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;
}
}
111 changes: 111 additions & 0 deletions src/HackThursday/Handler/FacebookHandler.php
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;
}

}

0 comments on commit 1d8bccd

Please sign in to comment.