【问题标题】:Upload video to facebook with php-sdk graph api使用 php-sdk graph api 将视频上传到 facebook
【发布时间】:2013-01-23 13:41:18
【问题描述】:

我在 facebook 文档中看到了一个示例。

$post_url = "https://graph-video.facebook.com/me/videos?"
. "title=" . $video_title. "&description=" . $video_desc 
. "&". $access_token;

但我想在 facebook php-sdk 中执行此操作。

$facebook->api('/me/videos/');

但视频服务器似乎是https://graph-video.facebook.com。 那么我如何使用 php-sdk 在图形 api 中做到这一点?

【问题讨论】:

    标签: php facebook facebook-graph-api facebook-php-sdk


    【解决方案1】:

    通过图表更简单

    你可以这样做:

    <?php
    $app_id = "YOUR_APP_ID";
    $app_secret = "YOUR_APP_SECRET";
    $my_url = "YOUR_POST_LOGIN_URL";
    $video_title = "TITLE FOR THE VIDEO";
    $video_desc = "DESCRIPTION FOR THE VIDEO";
    $group_id = "YOUR_GROUP_ID";
    
    $code = $_REQUEST["code"];
    
    echo '<html><body>';
    
    if(empty($code)) {
       $dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
         . $app_id . "&redirect_uri=" . urlencode($my_url)
         . "&scope=publish_stream";
        echo('<script>top.location.href="' . $dialog_url . '";</script>');
    }
    
    $token_url = "https://graph.facebook.com/oauth/access_token?client_id="
        . $app_id . "&redirect_uri=" . urlencode($my_url)
        . "&client_secret=" . $app_secret
        . "&code=" . $code;
    $access_token = file_get_contents($token_url);
    
    $post_url = "https://graph-video.facebook.com/" . $group_id . "/videos?"
        . "title=" . $video_title. "&description=" . $video_desc
        . "&". $access_token;
    
    echo '<form enctype="multipart/form-data" action=" '.$post_url.' "  
         method="POST">';
    echo 'Please choose a file:';
    echo '<input name="file" type="file">';
    echo '<input type="submit" value="Upload" />';
    echo '</form>';
    
    echo '</body></html>';
    ?>
    

    到你可以这样做的页面

    <?php
    $app_id = "YOUR_APP_ID";
    $app_secret = "YOUR_APP_SECRET";
    $my_url = "YOUR_POST_LOGIN_URL";
    $video_title = "TITLE FOR THE VIDEO";
    $video_desc = "DESCRIPTION FOR THE VIDEO";
    $page_id = "YOUR_PAGE_ID"; // Set this to your APP_ID for Applications
    
    $code = $_REQUEST["code"];
    
    echo '<html><body>';
    
    if(empty($code)) {
      // Get permission from the user to publish to their page. 
      $dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
        . $app_id . "&redirect_uri=" . urlencode($my_url)
        . "&scope=publish_stream,manage_pages";
      echo('<script>top.location.href="' . $dialog_url . '";</script>');
    } else {
    
      // Get access token for the user, so we can GET /me/accounts
      $token_url = "https://graph.facebook.com/oauth/access_token?client_id="
          . $app_id . "&redirect_uri=" . urlencode($my_url)
          . "&client_secret=" . $app_secret
          . "&code=" . $code;
      $access_token = file_get_contents($token_url);
    
      $accounts_url = "https://graph.facebook.com/me/accounts?" . $access_token;
      $response = file_get_contents($accounts_url);
    
      // Parse the return value and get the array of accounts we have
      // access to. This is returned in the data[] array. 
      $resp_obj = json_decode($response,true);
      $accounts = $resp_obj['data'];
    
      // Find the access token for the page to which we want to post the video.
      foreach($accounts as $account) {
           if($account['id'] == $page_id) {
             $access_token = $account['access_token'];
             break;
           }
      }
    
      // Using the page access token from above, create the POST action
      // that our form will use to upload the video.
      $post_url = "https://graph-video.facebook.com/" . $page_id . "/videos?"
          . "title=" . $video_title. "&description=" . $video_desc
          . "&access_token=". $access_token;
    
      // Create a simple form 
      echo '<form enctype="multipart/form-data" action=" '.$post_url.' "  
           method="POST">';
      echo 'Please choose a file:';
      echo '<input name="file" type="file">';
      echo '<input type="submit" value="Upload" />';
      echo '</form>';
    }
    
    echo '</body></html>';
    ?>
    

    【讨论】:

    • 使用这种编码,我怎样才能从 facebook 获得响应?
    • 提交后,您将收到“VIDEO_ID”,它会向您发送一个成功与否的对象,但根据视频的大小,上传可能需要大约 10 分钟
    • 谢谢。有用。上传几分钟后,视频会显示在 Facebook 上。正常吗?你知道如何标记该视频中的朋友吗?
    • 我不知道,因为我从来没有做过,你需要现在搜索,尽可能将你的问题标记为已回答:)
    • @RicardoVieira 你能为我们写新的 php-sdk 4.0 教程吗?我尝试将一些 fb 页面视频上传到我的 fb 页面上传。没有下载想要这个。这可能吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 2014-11-01
    • 1970-01-01
    • 2011-11-26
    相关资源
    最近更新 更多