cURL - 有没有办法上传视频到Facebook没有形式? [英] cURL - is there a way for upload a video to facebook without form?

查看:321
本文介绍了cURL - 有没有办法上传视频到Facebook没有形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

但我想上传一个视频到Facebook没有任何形式。它在Facebook上不起作用。

But i want to upload a video to facebook without any form. It doesn't work on 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
?>

如果我改变curl形成它的工作原理。

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


您建议什么?

推荐答案

这是一个更好的代码:

<?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. 很可能你的代码不能从localhost工作,除非你添加一个证书使用 curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,FALSE); 但使用后者是 NOT adviced(请参阅 this

  4. 检查 makeRequest() code>方法在 base_facebook.php

  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

这篇关于cURL - 有没有办法上传视频到Facebook没有形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆