用cron php发布到facebook页面

I have a job site where 100 jobs posting daily. i also have a Facebook page. i want to post 10 jobs each hour to My Facebook page with cron jobs. I am using the following code to post to Facebook face by accessing the URL Manually.

    <?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;
 ?>

It working fine. but i want to post the jobs automatically with corn jobs. how can i do it. does anyone have any advice?

doukengsha9472
doukengsha9472 看看这个类似的问题facebook.stackoverflow.com/questions/16503122/...
7 年多之前 回复

1个回答

Please see the codes below and edit/complete it according to yours

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' ) );
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问