Skip to content

Instantly share code, notes, and snippets.

@halkeye
Last active December 10, 2015 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halkeye/4351491 to your computer and use it in GitHub Desktop.
Save halkeye/4351491 to your computer and use it in GitHub Desktop.
<?php
require 'facebook-php-sdk/src/facebook.php';
ini_set('display_errors', true);
require '/home/halkeye/git/rob_facebook_poster/config.php';
/* FIXME - put your stuff here */
$facebook = new Facebook(array(
'appId' => $config['FACEBOOK_APP_ID'],
'secret' => $config['FACEBOOK_APP_SECRET'],
));
$user_id = $facebook->getUser();
if (!$user_id)
{
$login_url = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) );
echo 'Please <a href="' . $login_url . '">login.</a>';
die();
}
$url = @$_REQUEST['yt_url'];
if (empty($url))
{
echo "Please enter youtube url:";
echo "<form method='post' action='youtube.php'>";
echo "<br />";
echo "<input type='url' size='255' name='yt_url' value='http://www.youtube.com/watch?v=CpWUjImw4dU' />";
echo "<br />";
echo "<input type='submit' value='Post Youtube link to facebook' />";
echo "</form>";
die();
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, FALSE); // remove body
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$body = curl_exec($ch);
$doc=new DOMDocument();
@$doc->loadHTML($body);
$xml = @simplexml_import_dom($doc);
$meta=$xml->xpath('//meta');
$message = "Test Posting";
$fields = array(
'access_token' => $facebook->getAccessToken(),
'message' => 'Test Posting', // FIXME
'link' => null,
'source' => null,
'picture' => null,
);
foreach ($meta as $met)
{
if ($met['property'] == '') continue;
switch($met['property'])
{
case "og:title":
$fields['message'] = $met['content'] . "";
break;
/* TODO - if you want to include description somehow
case "og:description":
break;
*/
case "og:url":
$fields['link'] = $met['content']."";
break;
case "og:image":
$fields['picture'] = $met['content']."";
break;
case "og:video":
$fields['source'] = $met['content']."";
break;
}
}
$ret_obj = $facebook->api('/me/feed', 'POST', $fields);
echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment