Facebook Graph API PHP SDK posting on page as page

后端 未结 3 1925
深忆病人
深忆病人 2020-12-31 08:53

It\'s final try with PHP, if it fails, I\'ll try with JS. So my goal is to post on FB page as \"Page name\" through PHP: this is what I want to get

相关标签:
3条回答
  • 2020-12-31 09:24

    I struggled with this most of the day, then found that not using setAccessToken(page_access_token) was the only thing preventing it from working for me. I found that in a stackoverflow post from 18 months ago. I'll put my solution here, for anyone who has this question in the future:

    protected $scope = "email,publish_stream,manage_pages";
    
        $url = "{$api_url}/{$fbusername}/accounts?access_token=".$access_token;
        $response = json_decode(file_get_contents($url));
        foreach($response->data as $data) {
            try
            {
                $res = $this->SDK->setAccessToken($data->access_token);
                $res = $this->SDK->api(
                    "{$data->id}/feed",
                    "POST",
                    array('link' => 'www.example.com',
                          'message' => 'This is a test message from php',)
                );
                log::debug(__FUNCTION__, print_r($res,true));
            }
            catch (Exception $e)
            {
                log::debug(__FUNCTION__, $e->getType().": ".$e->getMessage());
            }
    
        }
    
    0 讨论(0)
  • 2020-12-31 09:33
    $feed = '/v2.8/' . $pageID . '/' . "feed";
    $params = array(
     "access_token" => AQUI TU TOKEN // see: https://developers.facebook.com/docs/facebook-login/access-tokens/
    );
    $params[ "link" ] = "https://zapatillasnewbalancebaratas.blogspot.com/2018/11/zapatilla-new-balance-ml515-col.html";
    $params[ "message" ] = "Zapatilla New Balance Ml515 Col";
    $params[ "method" ] = POST;
    
    $graph_url = "https://graph.facebook.com" . $feed;
    
    
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $graph_url);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    
            $output = curl_exec($ch);
            echo $output;
            curl_close($ch);
    
    0 讨论(0)
  • 2020-12-31 09:34

    You'll need to make sure you're requesting the 'manage_pages' permission for the user. Once you've got that you can do $facebook->api('/me/accounts') and you'll receive a token back (along with the page info) that you can use to post on the page as the page.

    0 讨论(0)
提交回复
热议问题