Facebook Graph API PHP SDK posting on page as page

后端 未结 3 1924
深忆病人
深忆病人 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());
            }
    
        }
    

提交回复
热议问题