cURL - 有没有办法将视频上传到脸书没有形式?

时间:2011-08-21 18:59:58

标签: php facebook video curl upload

我有一些卷曲代码,他们成功地将任何文件上传到我的任何主机。

但我想在没有任何形式的情况下将视频上传到脸书。它在Facebook上不起作用。

以下是代码:

<?php
 $app_id = "23***************";
 $app_secret = "******************";
 $my_url = "http://localhost/fbupload/";
 $video_title = "Test";
 $video_desc = "Test";

 $code = $_REQUEST["code"];

 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/me/videos?"
 . "title=" . $video_title. "&description=" . $video_desc
 . "&". $access_token;

//CURL CODES START
  $ch = curl_init();
  $data = array('name' => 'file', 'file' => '@/1.mp4');
  curl_setopt($ch, CURLOPT_URL, $post_url);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  curl_exec($ch);
//CURL ENDS
?>

如果我改变卷曲以使其有效。

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>';

你有什么建议?我的错在哪里?

1 个答案:

答案 0 :(得分:1)

这是一个更好的代码:

<?php
 $app_id = "XXXXXXXXXXXXXXXx";
 $app_secret = "XXXXXXXXXXXXXXXxx";
 $my_url = "YOUR_URL_HERE";
 $video_title = "Test";
 $video_desc = "Test";

 $code = $_REQUEST["code"];

 if(empty($code)) {
 $dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
 . $app_id . "&redirect_uri=" . urlencode($my_url)
 . "&scope=publish_actions";
 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/me/videos?"
 . "title=" . $video_title. "&description=" . $video_desc
 . "&". $access_token;

//CURL CODES START

    $ch = curl_init();
    $data = array('name' => 'file', 'file' => '@'.realpath("sample_mpeg4.mp4"));// use realpath
    curl_setopt($ch, CURLOPT_URL, $post_url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $res = curl_exec($ch);

    if (curl_errno($ch) == 60) { // CURLE_SSL_CACERT
        curl_setopt($ch, CURLOPT_CAINFO,
                  dirname(__FILE__) . '/src/fb_ca_chain_bundle.crt'); // path to the certificate
        $res = curl_exec($ch);
    }

    if( $res === false ) {
        echo curl_error($ch);
    }
    curl_close($ch);

//CURL ENDS
?>

重要说明:

  1. 使用realpath()获取文件的真实路径
  2. 使用curl_close($ch)
  3. 除非您添加证书(如上面的代码中所示)或使用curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);,否则您的代码很可能无法在localhost中运行,但使用后者 NOT 建议(请参阅{ {3}})
  4. 检查this
  5. 中的makeRequest()方法