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

报告相同问题?

悬赏问题

  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿