Facebook – Introduzione alle Graph API

facebook_2015_logo_detail

Cose necessarie:

  • ID dell’utente
  • Access token
  • CURL installato nel nostro server
Procurarsi un access token
https://www.facebook.com/dialog/oauth?client_id=xxx&redirect_uri=xxx&response_type=token&scope=create_event

http://www.venetotornei.com/#access_token=xxx&expires_in=4101

https://graph.facebook.com/xxx?access_token=xxx
<?php
$userID = "XXX"; // Può essere me o un numero
$accessToken = "XXX";
$update_url = "https://graph.facebook.com/".$userID."/events";
$form_fields = array(
	"access_token" => $accessToken,
	"start_time" => time() + (7 * 24 * 60 * 60),
	"end_time" => time() + (8 * 24 * 60 * 60),
	"location" => "someplace",
	"name" => "New Test Event Using Graph API"
);

$temp = array();

foreach( $form_fields as $key => $value )
	$temp[$key]=utf8_encode($value);

$form_data = http_build_query($temp);

$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, $update_url);
curl_setopt ($curl, CURLOPT_POST, 1);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $form_data);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);

$result = json_decode(res.content);

if ($result["id"]!==false)
   echo "Successfully Created Event";
else
   echo "Failure";

?>

E come sempre: scusate la rozzezza del codice.

Buona “developpata”! 😉