Last active
December 10, 2015 00:29
-
-
Save halkeye/4351491 to your computer and use it in GitHub Desktop.
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 | |
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