facebook使用php自动发布帖子

时间:2015-08-26 15:19:15

标签: php mysql facebook facebook-graph-api

我正在开发一个在Facebook上自动共享帖子的PHP脚本。当我直接从浏览器执行时,我的脚本运行正常,但是当我启用cron作业时,它基本上执行文件但不在我的Facebook墙上共享。我不确切地知道它背后的问题是什么。这是我的剧本:

<?php
ob_start();
require_once('facebookapi/site-primary-config.php');
require_once('facebookapi/facebook.php');

// configuration
 $appid = 'xxxxx';
 $appsecret = 'xxxxx';
 $pageId = 'xxxx';

//Get Load from Social Video ID 
$get_link_sql = mysqli_query($conn,"SELECT
tblmevids.vid_code as video_tbl_code,
tblmevids.vid_title as video_tbl_title,
tblmevids.vid_seo_descp as video_tbl_description,
tblmevids.vid_slug as video_tbl_slug,
tblsocial_share.vidsc_code as vid_social_code,
tblsocial_share.vid_fb_sstat as vid_social_fb_status
FROM tblmevids
JOIN tblsocial_share
ON tblmevids.vid_code = tblsocial_share.vidsc_code
WHERE tblmevids.vid_act_stat = '1' AND tblsocial_share.vid_fb_sstat = '0' LIMIT 1");
$count_sql = mysqli_num_rows($get_link_sql);
$link_row = mysqli_fetch_array($get_link_sql);
$selected_video = $link_row['video_tbl_code'];
$selected_title = $link_row['video_tbl_title'];
$selected_description = $link_row['video_tbl_description'];
$selected_slug = $link_row['video_tbl_slug'];
    if($count_sql > 0 ) {
        //Grab Video Details
            $msg = $selected_title.' - Watch Now at '.$site_baseurl.'watch?v='.$selected_video;
            $title = $selected_title;
            $uri = $site_baseurl.$selected_slug;
            $desc = $selected_description;
            $pic = $site_baseurl.'video_thumbs/'.$selected_video.'.jpg';
            $action_name = 'Go to mywebsite.com';
            $action_link = 'http://mywebsite.com';

            echo $msg."<br/>".$title."<br/>".$uri."<br/>".$desc."<br/>".$pic."<br/>".$action_name."<br/>".$action_link."<br/>";

            //Start FB Sharing
            $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);
//Update Social Table Start
$get_perfect_status = '1';
$updates = array();
    if (is_numeric($get_perfect_status))
      $updates[] = '`vid_fb_sstat` ="'.$get_perfect_status.'"';
      $updates = implode(', ', $updates); 
      $sql = "UPDATE `tblsocial_share` SET $updates WHERE vidsc_code = '$selected_video'";
    $result=mysqli_query($conn,$sql);
        if($result){
                echo "Shared on Facebook and updated sucessfully";
        } else {
                echo "There was a problem in sharing";
        }
//Update Social Table End
                     } 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' => 'publish_actions,manage_pages'))}");
                     }

                    echo $status;

    } else {
        echo "not found";
    }
 ?>

1 个答案:

答案 0 :(得分:0)

当你在一个crons工作中运行它时,它没有得到用户,当你在浏览器中运行它时,你在浏览器上登录到facebook。解决这个问题的方法是从Facebook获取用户ID并更改

$user = $facebook->getUser();

要     $ user ='xxxxx';

在$ user = $ facebook-&gt; getUser();

之后,您可以使用简单的echo语句获取您的用户ID
echo $user."\n";