Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Facebook API-社区组中的发布为页面_Php_Facebook_Facebook Graph Api_Facebook Php Sdk - Fatal编程技术网

Php Facebook API-社区组中的发布为页面

Php Facebook API-社区组中的发布为页面,php,facebook,facebook-graph-api,facebook-php-sdk,Php,Facebook,Facebook Graph Api,Facebook Php Sdk,通过使用groups()连接页面的新特性,我认为可以通过graphapi在组提要上发布为页面。我所有的尝试都以以下错误告终: 页面上没有边缘/节点“组” require_once.“/vendor/autoload.php”; 使用Facebook\Facebook; 使用Facebook\Exceptions\FacebookResponseException; 使用Facebook\Exceptions\FacebookSDKException; 使用Facebook\Authenticat

通过使用groups()连接页面的新特性,我认为可以通过graphapi在组提要上发布为页面。我所有的尝试都以以下错误告终:

页面上没有边缘/节点“组”

require_once.“/vendor/autoload.php”;
使用Facebook\Facebook;
使用Facebook\Exceptions\FacebookResponseException;
使用Facebook\Exceptions\FacebookSDKException;
使用Facebook\Authentication\OAuth2Client;
使用Facebook\Authentication\AccessToken;
使用Facebook\Helpers\FacebookRedirectLoginHelper;
$fb=新Facebook([
“应用程序id”=>“123456”,
“app_secret”=>“abcde”,
“默认图形版本”=>“v2.10”,
//“默认\u访问\u令牌”=>页面\u令牌,//可选
]);
$helper=$fb->getCanvasHelper();
$permissions=['user\u managed\u group'、'publish\u actions'、'manage\u pages'、'publish\u pages'];
$tokenFileName=“./fb token.txt”;
$date=新的日期时间();
$cityToPost='Berlin';
$autopostGroups=array();
$message=数组(
'消息'=>'测试帖子:'.$date->getTimestamp(),
'链接'=>'https://domain.de'
);
试一试{
//使用新的longlicedaccesstoken刷新longlicedaccesstoken
如果(文件_存在($tokenFileName)){
$token=file\u get\u contents($tokenFileName);
$lastTokenRefresh=time()-filemtime($tokenFileName);
如果($lastTokenRefresh>60*60*24*7)
{
$oAuth2Client=$fb->getOAuth2Client();
$newToken=$oAuth2Client->getAccessTokenFromCode(
$oAuth2Client->getCodeFromLongLivedAccessToken(
美元代币
)
);
文件内容($tokenFileName,(string)$newToken);
$accessToken=(字符串)$newToken;
}否则{
$accessToken=(字符串)$token;
}
}否则{
$accessToken=$helper->getAccessToken();
}
}捕获(FacebookResponseException$e){
//当图形返回错误时
echo“图形返回错误:”。$e->getMessage();
取消链接($tokenFileName);
出口
}捕获(FacebookSDKException$e){
//验证失败或其他本地问题时
echo“Facebook SDK返回错误:”。$e->getMessage();
取消链接($tokenFileName);
出口
}
if(isset($accessToken)){
如果(文件_存在($tokenFileName)){
$fb->setDefaultAccessToken(文件获取内容($tokenFileName));
}否则{
//OAuth2.0客户端处理程序
$oAuth2Client=$fb->getOAuth2Client();
//将短期访问令牌交换为长期访问令牌
$longLivedAccessToken=$oAuth2Client->getLongLivedAccessToken((字符串)$accessToken);
文件内容($tokenFileName,(string)$longLivedAccessToken);
$fb->setDefaultAccessToken($longLivedAccessToken);
}
//当页面收到$\u GET['code']变量时,将用户重定向回应用程序
如果(isset($_GET['code'])){
echo“window.top.location.href=”https://apps.facebook.com/xyz/';";
出口
}
//正在验证访问令牌
试一试{
$request=$fb->get('/me');
}捕获(FacebookResponseException$e){
//当图形返回错误时
如果($e->getCode()==190){
取消链接($tokenFileName);
//取消设置($_会话['facebook_访问令牌]);
$helper=$fb->getRedirectLoginHelper();
$loginUrl=$helper->getLoginUrl('https://apps.facebook.com/xyz/“,$permissions);
echo“window.top.location.href=”$loginUrl。“”;
出口
}
}捕获(FacebookSDKException$e){
//验证失败或其他本地问题时
echo“Facebook SDK返回错误:”。$e->getMessage();
出口
}
//获取由用户管理的组的列表
试一试{
$requestGroups=$fb->get('/me/groups');
$groups=$requestGroups->getGraphEdge()->asArray();
}捕获(FacebookResponseException$e){
//当图形返回错误时
echo“图形返回错误:”。$e->getMessage();
出口
}捕获(FacebookSDKException$e){
//验证失败或其他本地问题时
echo“Facebook SDK返回错误:”。$e->getMessage();
出口
}
foreach($组作为$组){
if(strpos($group['name'],'VENDOR')!==false和&strpos($group['name'],$cityToPost)!==false){
array_push($autopostGroups,$group['id']);//仅用于调试
试一试{
$requestPost=$fb->post('/'.$group['id']./feed',$message);
$post=$requestPost->getGraphNode()->asArray();
var_dump(员额);
}捕获(FacebookResponseException$e){
//当图形返回错误时
echo“图形返回错误:”。$e->getMessage();
出口
}捕获(FacebookSDKException$e){
//验证失败或其他本地问题时
echo“Facebook SDK返回错误:”。$e->getMessage();
出口
}
}
}
//现在,您可以重定向到另一个页面并使用$tokenFileName中的访问令牌
}否则{
$helper=$fb->getRedirectLoginHelper();
$loginUrl=$helper->getLoginUrl('https://apps.facebook.com/xyz/“,$permissions);
echo“window.top.location.href=”$loginUrl。“”;
}
我是FBAPI的新手,我不是英语母语的人,所以也许有人能澄清这种情况

谢谢大家!

[编辑1]

添加了代码。提示:当我将
PAGE\u-TOKEN
更改为
USER\u-TOKEN
时,它会工作

[编辑2]

完整的代码添加。 我在8月份的FB群中发现了一条用户评论:

不,没有内置的方式让页面帖子显示在一个组中 自动地


请提供用于生成该错误的代码@对不起,你说得对!您没有尝试访问t中页面id上的
节点
require_once "./vendor/autoload.php";

use Facebook\Facebook;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;
use Facebook\Authentication\OAuth2Client;
use Facebook\Authentication\AccessToken;
use Facebook\Helpers\FacebookRedirectLoginHelper;

$fb = new Facebook([
    'app_id' => '123456',
    'app_secret' => 'abcde',
    'default_graph_version' => 'v2.10',
    //'default_access_token' => PAGE_TOKEN, // optional
]);

$helper = $fb->getCanvasHelper();
$permissions = ['user_managed_groups', 'publish_actions', 'manage_pages', 'publish_pages'];

$tokenFileName = "./fb-token.txt";
$date = new DateTime();
$cityToPost = 'Berlin';
$autopostGroups = array();
$message = array(
    'message' => 'TEST Post: '.$date->getTimestamp(),
    'link' => 'https://domain.de'
);

try {
    // Refresh longlicedaccesstoken with new one
    if (file_exists($tokenFileName)) {
        $token = file_get_contents($tokenFileName);
        $lastTokenRefresh = time() - filemtime($tokenFileName);
        if ($lastTokenRefresh > 60 * 60 * 24 * 7)
        {
            $oAuth2Client  = $fb->getOAuth2Client();
            $newToken      = $oAuth2Client->getAccessTokenFromCode(
                $oAuth2Client->getCodeFromLongLivedAccessToken(
                    $token
                )
            );
            file_put_contents($tokenFileName, (string) $newToken);
            $accessToken                       = (string) $newToken;
        }else{
            $accessToken                       = (string) $token;
        }
    } else {
        $accessToken = $helper->getAccessToken();
    }
} catch(FacebookResponseException $e) {
    // When Graph returns an error
    echo 'Graph returned an error: ' . $e->getMessage();
    unlink($tokenFileName);
    exit;
} catch(FacebookSDKException $e) {
    // When validation fails or other local issues
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
    unlink($tokenFileName);
    exit;
}

if (isset($accessToken)) {
    if (file_exists($tokenFileName)) {
        $fb->setDefaultAccessToken(file_get_contents($tokenFileName));
    } else {
        // OAuth 2.0 client handler
        $oAuth2Client = $fb->getOAuth2Client();
        // Exchanges a short-lived access token for a long-lived one
        $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken((string) $accessToken);
        file_put_contents($tokenFileName, (string) $longLivedAccessToken);
        $fb->setDefaultAccessToken($longLivedAccessToken);
    }
    // redirect user back to app when page receives $_GET['code'] variable
    if (isset($_GET['code'])) {
        echo "<script>window.top.location.href='https://apps.facebook.com/xyz/';</script>";
        exit;
    }

    // validating the access token
    try {
        $request = $fb->get('/me');
    } catch(FacebookResponseException $e) {
        // When Graph returns an error
        if ($e->getCode() == 190) {
            unlink($tokenFileName);
            //unset($_SESSION['facebook_access_token']);
            $helper = $fb->getRedirectLoginHelper();
            $loginUrl = $helper->getLoginUrl('https://apps.facebook.com/xyz/', $permissions);
            echo "<script>window.top.location.href='".$loginUrl."'</script>";
            exit;
        }
    } catch(FacebookSDKException $e) {
        // When validation fails or other local issues
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
        exit;
    }
    // get list of groups managed by user
    try {
        $requestGroups = $fb->get('/me/groups');
        $groups = $requestGroups->getGraphEdge()->asArray();

    } catch(FacebookResponseException $e) {
        // When Graph returns an error
        echo 'Graph returned an error: ' . $e->getMessage();
        exit;
    } catch(FacebookSDKException $e) {
        // When validation fails or other local issues
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
        exit;
    }
    foreach ($groups as $group) {
        if(strpos($group['name'], 'VENDOR') !== false && strpos($group['name'], $cityToPost) !== false){
            array_push($autopostGroups,$group['id']); //Only for debugging

            try {
                $requestPost = $fb->post('/'.$group['id'].'/feed', $message);
                $post = $requestPost->getGraphNode()->asArray();

                var_dump($post);
            } catch(FacebookResponseException $e) {
                // When Graph returns an error
                echo 'Graph returned an error: ' . $e->getMessage();
                exit;
            } catch(FacebookSDKException $e) {
                // When validation fails or other local issues
                echo 'Facebook SDK returned an error: ' . $e->getMessage();
                exit;
            }
        }

    }
    // Now you can redirect to another page and use the access token from $tokenFileName
} else {
    $helper = $fb->getRedirectLoginHelper();
    $loginUrl = $helper->getLoginUrl('https://apps.facebook.com/xyz/', $permissions);
    echo "<script>window.top.location.href='".$loginUrl."'</script>";
}