cURL - 有没有一种方法可以在没有表单的情况下将视频上传到 Facebook?

发布于 2024-11-30 11:02:30 字数 1415 浏览 0 评论 0原文

我有一些curl代码,它们可以成功地将任何文件上传到我的任何主机。

但我想在没有任何形式的情况下将视频上传到 Facebook。它在 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('名称' => '文件', '文件' => '@/1.mp4'); curl_setopt($ch, CURLOPT_URL, $post_url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 卷曲执行($ch);
//CURL ENDS
?>

如果我改变curl来形成它就可以了。

echo '

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

I have some curl codes and they upload any file to any host of me successfully.

But i want to upload a video to facebook without any form. It doesn't work on facebook.

Here are codes:

<?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
?>

if i change curl to form it works.

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

What do you suggest? where is my fault?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安穩 2024-12-07 11:02:30

这是一个更好的代码:

<?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); ,否则代码将无法在本地主机上工作,但使用后者是建议(请参阅
  4. 检查< base_facebook.code>makeRequest() 方法。 php

Here's a better code:

<?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
?>

IMPORTANT NOTES:

  1. Use realpath() to get the real path of the file
  2. use curl_close($ch)
  3. Most likely your code won't work from localhost unless you add a certificate (as in the code above) or using curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); but using the later is NOT adviced (refer to this)
  4. Check the makeRequest() method in base_facebook.php
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文