我有一个工作网站,每天发布100个工作。我也有一个Facebook页面。我想每小时发布10个工作到我的Facebook页面与cron工作。 我正在使用以下代码通过手动访问URL发布到Facebook脸。
<?php
require_once 'facebook_sdk/src/facebook.php';
// configuration
$appid = 'xxxxxxx';
$appsecret = 'xxxxxxx';
$pageId = 'xxxxx';
$msg = 'test';
$title = 'pagetitle';
$uri = 'http://google.com/';
$desc = 'description here';
$pic = 'http://google.com/test/2.png';
$action_name = 'Go to my site';
$action_link = 'http://www.google.com';
$facebook = new Facebook(array(
'appId' => $appid,
'secret' => $appsecret,
'cookie' => false,
));
$user = $facebook->getUser();
// Contact Facebook and get token
if ($user) {
// you're logged in, and we'll get user acces token for posting on the wall
try {
$page_info = $facebook->api("/$pageId?fields=access_token");
if (!empty($page_info['access_token'])) {
$attachment = array(
'access_token' => $page_info['access_token'],
'message' => $msg,
'name' => $title,
'link' => $uri,
'description' => $desc,
'picture'=>$pic,
'actions' => json_encode(array('name' => $action_name,'link' => $action_link))
);
$status = $facebook->api("/$pageId/feed", "post", $attachment);
} else {
$status = 'No access token recieved';
}
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
} else {
// you're not logged in, the application will try to log in to get a access token
header("Location:{$facebook->getLoginUrl(array('scope' => 'photo_upload,user_status,publish_stream,user_photos,manage_pages'))}");
}
echo $status;
?>
工作正常。但我想用玉米工作自动发布工作。我该怎么做。 有人有建议吗?
答案 0 :(得分:1)
请参阅以下代码并根据您的编辑/完成
class Facebook
{
/**
* @var The page id to edit
*/
private $page_id = '_PAGE_ID_';
/**
* @var the page access token given to the application above
*/
private $page_access_token = '_ACCESS_TOKEN_';
/**
* @var The back-end service for page's wall
*/
private $post_url = '';
/**
* Constructor, sets the url's
*/
public function Facebook()
{
$this->post_url = 'https://graph.facebook.com/'.$this->page_id.'/feed';
}
public function renew_access()
{
$url = 'https://graph.facebook.com/oauth/access_token?client_id=CLIENT_ID&client_secret=CLIENT_SECRET&display=popup&code=_CODE_&redirect_uri=THIS_FILE_FULL_URL'; // THIS_FILE_FULL_URL = like http://site.com/fb.post.php
// request this url to renew access token to send posts when offline. get the access_token and set self::$page_access_token = _ACCESS_TOKEN_
}
private function getcode()
{
$url = 'https://www.facebook.com/dialog/oauth?client_id=CLIENT_ID&redirect_uri=THIS_FILE_FULL_URL'; // THIS_FILE_FULL_URL = like http://site.com/fb.post.php
// request this url to get _CODE_ to send posts when offline. then get the access_token and set self::$page_access_token = _ACCESS_TOKEN_
}
private function want_to_send()
{
// check for somethings if you want to send or you don't
// for eg. check for time or any other check if sent before, or just return true to pass
return true;
}
public function message($data)
{
// need token
$data['access_token'] = $this->page_access_token;
if(!$data['properties'])
$data['properties'] = '{"TITLE":"DESC"}';
try{
if(self::want_to_send())
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$this->post_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_USERAGENT , 'facebook-php-3.1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to suppress the curl output
$return = curl_exec($ch);
curl_close ($ch);
}
}
catch ( exception $e){
//throw new Exception($e);
// or
error_log(json_encode($data));
}
//return $return; // if you want return
}
}
$facebook = new Facebook();
// make a simple post test
$facebook->message(array( 'message' => 'The status header',
'link' => 'http://cekirdek.com.tr',
'picture' => 'http://domain.com/picture_url.png',
'name' => 'Name of the picture, shown just above it',
'description' => 'Full description explaining whether the header or the picture' ) );