Facebook页面上的PHP自动发布

时间:2015-08-21 11:21:52

标签: php facebook facebook-graph-api

我正在尝试使用我的应用在Facebook页面上自动发布,如果我将 my-page-numeric-id 替换为我的个人资料数字ID,但是当我将我的页面数字ID改为 my-page-numeric-id 并将有效访问权限 n无法在Facebook页面上发布我尝试提交审核权限,如下所示&# 34; manage_pages,publish_actions"但Facebook团队提到您并不需​​要这一切,因为您拥有此页面和您当前的管理员,当任何其他人需要使用您的应用程序进行授权时,这些权限是必需的。 现在我的问题是,我的代码完全适用于在我的个人资料墙上发布帖子,但我无法在我的页面上自动发布它背后的问题我无法弄清楚这里是我的代码

<?php
 require_once 'src/facebook.php';
class FacebookApi {

    var $consumer;
    var $token;
    var $method;
    var $http_status;
    var $last_api_call;
    var $callback;
    var $connection;
    var $access_token;

    function __construct($data){
        $config = array();
        $config['appId'] = $data['consumer_key'];
        $config['secret'] = $data['consumer_secret'];

        $this->connection = new Facebook($config);

    }

    function share($title, $targetUrl, $imgUrl, $description, $access_token){

        $this->connection->setAccessToken($access_token);

        $params["access_token"] = $access_token;
        if(!empty($title)){
            $params["message"] = $title;
            $params["name"] = $title;
        }
        if(!empty($targetUrl)){
            $params["link"] = $targetUrl;
        }
        if(!empty($imgUrl)){
            $params["picture"] = $imgUrl;
        }
        if(!empty($description)){
            $params["description"] = $description;
        }

        // post to Facebook
        try {
          $ret = $this->connection->api('/my-page-numeric-id/feed', 'POST', $params);
        } catch(Exception $e) {
          $e->getMessage();
        }

        return true;
    }

    function getLoginUrl($params){
        return $this->connection->getLoginUrl($params);
    }

    function getContent($url) {
        $ci = curl_init();
        /* Curl settings */
        curl_setopt($ci, CURLOPT_URL, $url);
        curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ci, CURLOPT_HEADER, false);
        curl_setopt( $ci, CURLOPT_CONNECTTIMEOUT, 10 );

        curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);

        $response = curl_exec($ci);
        curl_close ($ci);
        return $response;
      }
}

$access_token = 'long-live-token-here';
$facebookData = array();
$facebookData['consumer_key'] = 'app-id-here';
$facebookData['consumer_secret'] = 'app-secret-here';

$title = "Demo Content Posted on Timeline";
$targetUrl = "http://www.demo_url.com/1234-post";
$imgUrl = "http://www.demo_url.com/1234-post-image.png";
$description = "demo_description_here"; 

$facebook = new FacebookApi($facebookData);
$facebook->share($title, $targetUrl, $imgUrl, $description, $access_token);

?>

2 个答案:

答案 0 :(得分:4)

使用API​​ v2.3,他们将发布权限分为两个单独的:

  • publish_actions适用于您要将作为用户发布的任何内容

  • 如果您要作为网页发布,则需要publish_pages权限(除了manage_pages

答案 1 :(得分:1)

现在是facebook v5 sdk的时间

$fb = new Facebook\Facebook([
  'app_id' => 'app_id',
  'app_secret' => 'app_secret',
  'default_graph_version' => 'v2.8',
  ]);
             // facebook auto post

$params = array(
  "message" => "$title in $merchant   $short",
  "link" => "http://pickmyoffers.com/",
  "picture" => "http://Pickmyoffers.com/images/searched/Flipkart.png",
  "name" => "www.Pickmyoffers.com",
  "caption" => "www.pickmyoffers.com",
  "description" => "Submit Coupon and earn money through Pickmyoffers.com | Deals,Coupons and offers."
);
            $post = $fb->post('/Page_id/feed',$params, $access_token);
            $post = $post->getGraphNode()->asArray();

}

希望得到帮助