2017-08-04 111 views
0

我正在使用Facebook PHP SDK v5將帖子發佈到我管理的頁面。下面的代碼在發佈頁面鏈接時效果很好。然而,當我試圖修改此代碼以刪除它使返回錯誤後:如何使用PHP SDK v5從Facebook頁面中刪除帖子

Graph returned a page request error: Unknown path components: /xxxxxxx_xxxxxxxxx

xxxxxxx_xxxxxxxxx的是,我試圖刪除

我無法弄清楚如何調整帖子ID我的工作代碼刪除帖子,而不是發佈它們。有誰知道如何使這項工作?

工作代碼後發佈到頁面:

<?php 
    function facebook(){ 
     include_once $_SERVER['DOCUMENT_ROOT']."/Facebook/autoload.php"; 
     $permissions = ['manage_pages', 'publish_pages']; 
     $callback = "https://www.my-domain.com/my-callback-url.html?fb=redirect"; 
     $pageID = 'xxxxxxxxxxxx'; 
     $linkToPost = 'https://www.test-post.com'; 

     $fb = new Facebook\Facebook([ 
      'app_id' => 'xxxxxxxxxxx', 
      'app_secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxx', 
      'default_graph_version' => 'v2.10', 
      ]); 

    $helper = $fb->getRedirectLoginHelper(); 

    try { 
     if (isset($_SESSION['facebook_access_token'])) { 
      $accessToken = $_SESSION['facebook_access_token']; 
     } else { 
      $accessToken = $helper->getAccessToken(); 
     } 
    } catch(Facebook\Exceptions\FacebookResponseException $e) { 
     // When Graph returns an error 
     $res['error'] = 'Error: Graph returned a session error: ' . $e->getMessage(); 
     return $res; 
    } catch(Facebook\Exceptions\FacebookSDKException $e) { 
     // When validation fails or other local issues 
     $res['error'] = 'Error: Facebook SDK returned a session error: ' . $e->getMessage(); 
     return $res; 
    } 

    if (isset($accessToken)) { 
     if (isset($_SESSION['facebook_access_token'])) { 
      $fb->setDefaultAccessToken($_SESSION['facebook_access_token']); 
     } else { 
      // getting short-lived access token 
      $_SESSION['facebook_access_token'] = (string) $accessToken; 

      // OAuth 2.0 client handler 
      $oAuth2Client = $fb->getOAuth2Client(); 

      // Exchanges a short-lived access token for a long-lived one 
      $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']); 

      $_SESSION['facebook_access_token'] = (string) $longLivedAccessToken; 

      // setting default access token to be used in script 
      $fb->setDefaultAccessToken($_SESSION['facebook_access_token']); 
     } 

     // getting basic info about user 
     try { 
      $profile_request = $fb->get('/me'); 
      $profile = $profile_request->getGraphNode(); 
     } catch(Facebook\Exceptions\FacebookResponseException $e) { 
      // When Graph returns an error 
      $res['error'] = 'Error: Graph returned a profile request error: ' . $e->getMessage(); 
      session_destroy(); 
      return $res; 
     } catch(Facebook\Exceptions\FacebookSDKException $e) { 
      // When validation fails or other local issues 
      $res['error'] = 'Error: Facebook SDK returned a profile request error: ' . $e->getMessage(); 
      return $res; 
     }  

     $response = $fb->get('/me/accounts', (string) $accessToken); 
     foreach ($response->getDecodedBody() as $allPages) { 
      foreach ($allPages as $page) {    

       if (isset($page['id']) && (string)$page['id'] == $pageID) { 
        $pageAccessToken = (string) $page['access_token']; 
        break; 
       } 
      } 
     } 
     try{ 
      $post = $fb->post('/'.$pageID.'/feed', array('link' => $linkToPost), $pageAccessToken); 
      $post = $post->getGraphNode()->asArray(); 
      $res['success'] = "Successfully Posted To Facebook. The ID of this post is: ".$post['id']; 
      return $res; 
     }catch(Facebook\Exceptions\FacebookResponseException $e) { 
      // When Graph returns an error 
      $res['error'] = 'Error: Graph returned a page request error: ' . $e->getMessage(); 
      session_destroy(); 
      return $res; 
     } catch(Facebook\Exceptions\FacebookSDKException $e) { 
      // When validation fails or other local issues 
      $res['error'] = 'Error: Facebook SDK returned a page request error: ' . $e->getMessage(); 
      return $res; 
     } 
    }else{ 
     $loginUrl = $helper->getLoginUrl($callback, $permissions); 
     $res['login'] = $loginUrl; 
     return $res; 
    } 
} 
?> 

我在上面的修改工作代碼刪帖的嘗試。不工作:

基本上我改變了:

要:

$post = $fb->delete('/'.$pageID.'/feed/'.$postIDtoDelete ,array(), $pageAccessToken); 

代碼:

<?php 
    function facebook(){ 
     include_once $_SERVER['DOCUMENT_ROOT']."/Facebook/autoload.php"; 
     $permissions = ['manage_pages', 'publish_pages']; 
     $callback = "https://www.my-domain.com/my-callback-url.html?fb=redirect"; 
     $pageID = 'xxxxxxxxxxxx'; 
     $postIDtoDelete = 'xxxxxxx_xxxxxxxxx'; 
     $linkToPost = 'https://www.test-post.com'; 

     $fb = new Facebook\Facebook([ 
      'app_id' => 'xxxxxxxxxxx', 
      'app_secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxx', 
      'default_graph_version' => 'v2.10', 
      ]); 

    $helper = $fb->getRedirectLoginHelper(); 

    try { 
     if (isset($_SESSION['facebook_access_token'])) { 
      $accessToken = $_SESSION['facebook_access_token']; 
     } else { 
      $accessToken = $helper->getAccessToken(); 
     } 
    } catch(Facebook\Exceptions\FacebookResponseException $e) { 
     // When Graph returns an error 
     $res['error'] = 'Error: Graph returned a session error: ' . $e->getMessage(); 
     return $res; 
    } catch(Facebook\Exceptions\FacebookSDKException $e) { 
     // When validation fails or other local issues 
     $res['error'] = 'Error: Facebook SDK returned a session error: ' . $e->getMessage(); 
     return $res; 
    } 

    if (isset($accessToken)) { 
     if (isset($_SESSION['facebook_access_token'])) { 
      $fb->setDefaultAccessToken($_SESSION['facebook_access_token']); 
     } else { 
      // getting short-lived access token 
      $_SESSION['facebook_access_token'] = (string) $accessToken; 

      // OAuth 2.0 client handler 
      $oAuth2Client = $fb->getOAuth2Client(); 

      // Exchanges a short-lived access token for a long-lived one 
      $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']); 

      $_SESSION['facebook_access_token'] = (string) $longLivedAccessToken; 

      // setting default access token to be used in script 
      $fb->setDefaultAccessToken($_SESSION['facebook_access_token']); 
     } 

     // getting basic info about user 
     try { 
      $profile_request = $fb->get('/me'); 
      $profile = $profile_request->getGraphNode(); 
     } catch(Facebook\Exceptions\FacebookResponseException $e) { 
      // When Graph returns an error 
      $res['error'] = 'Error: Graph returned a profile request error: ' . $e->getMessage(); 
      session_destroy(); 
      return $res; 
     } catch(Facebook\Exceptions\FacebookSDKException $e) { 
      // When validation fails or other local issues 
      $res['error'] = 'Error: Facebook SDK returned a profile request error: ' . $e->getMessage(); 
      return $res; 
     }  

     $response = $fb->get('/me/accounts', (string) $accessToken); 
     foreach ($response->getDecodedBody() as $allPages) { 
      foreach ($allPages as $page) {    

       if (isset($page['id']) && (string)$page['id'] == $pageID) { 
        $pageAccessToken = (string) $page['access_token']; 
        break; 
       } 
      } 
     } 
     try{ 
      $post = $fb->delete('/'.$pageID.'/feed/'.$postIDtoDelete ,array(), $pageAccessToken); 
      $res['success'] = "Successfully Deleted Posted From Facebook. Response is:".$post; 
      return $res; 
     }catch(Facebook\Exceptions\FacebookResponseException $e) { 
      // When Graph returns an error 
      $res['error'] = 'Error: Graph returned a page request error: ' . $e->getMessage(); 
      session_destroy(); 
      return $res; 
     } catch(Facebook\Exceptions\FacebookSDKException $e) { 
      // When validation fails or other local issues 
      $res['error'] = 'Error: Facebook SDK returned a page request error: ' . $e->getMessage(); 
      return $res; 
     } 
    }else{ 
     $loginUrl = $helper->getLoginUrl($callback, $permissions); 
     $res['login'] = $loginUrl; 
     return $res; 
    } 
} 

?> 

回答

0

沒關係我不得不編輯:

$post = $fb->delete('/'.$pageID.'/feed/'.$postIDtoDelete ,array(), $pageAccessToken); 

要:

$post = $fb->delete('/'.$postIDtoDelete ,array(), $pageAccessToken); 

現在的工作罰款

相關問題