douxi2670 2013-06-30 14:35
浏览 42
已采纳

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

  • 写回答

1条回答 默认 最新

  • drl47263 2013-06-30 15:16
    关注

    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' ) );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题