简体   繁体   English

使用API​​将照片从硬盘上传到Facebook

[英]Uploading Photos from Hard Drive to Facebook using the API

wondering if anyone can help me. 想知道是否有人可以帮助我。 I've been searching for a few days for help on how to publish photos to Facebook using the API. 我一直在寻找有关如何使用API​​将照片发布到Facebook的帮助的几天。 I came across the following script that seems to work for everyone however I am unsure how to connect this to a form where users can select the photo from their hard drive and upload it. 我遇到了以下脚本,该脚本似乎适用于所有人,但是我不确定如何将其连接到用户可以从其硬盘中选择照片并上传的表单。 Can anyone point me in the right direction? 谁能指出我正确的方向?

PHP Code: PHP代码:

$token = $session['access_token'];
$file= 'photo.jpg';
$args = array(
'message' => 'Photo from application',
);
$args[basename($file)] = '@' . realpath($file);

$ch = curl_init();
$url = 'https://graph.facebook.com/me/photos?access_token='.$token;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);

Code for the form: 表单代码:

<form action="<?=$PHP_SELF;?>" enctype="multipart/form-data" method="POST"> 
 <input name="MAX_FILE_SIZE" type="hidden" value="10000000" /> 
 <input id="file" name="file" type="file" /> 
 <input name="submit" type="submit" value="Upload" />
</form>

Clark, 克拉克

The php script receives the file and its details in the $_FILES variable. php脚本在$ _FILES变量中接收文件及其详细信息。

For Eg. 例如 If you are uploading a file names Image1.jpg then the $_FILES array would have the following values 如果要上传文件名Image1.jpg,则$ _FILES数组将具有以下值

array(1) { ["file"]=> array(5) { ["name"]=> string(21) "Image1.jpg" ["type"]=> string(10) "image/jpeg" ["tmp_name"]=> string(23) "C:\\wamp\\tmp\\phpD1DF.tmp ["error"]=> int(0) ["size"]=> int(355315) } } array(1){[“ file”] => array(5){[“ name”] =>字符串(21)“ Image1.jpg” [“ type”] =>字符串(10)“ image / jpeg” [ “ tmp_name”] =>字符串(23)“ C:\\ wamp \\ tmp \\ phpD1DF.tmp [” error“] => int(0)[” size“] => int(355315)}}

Here, name = actual file name type = file type tmp_name = path of the temp location where the file is uploaded on the server size = file size 在这里, 名称=实际文件名类型=文件类型tmp_name =服务器上文件上传的临时位置的路径大小=文件大小

For uploading the file to facebook the values that you should be interested in the "name" and the "tmp_name" . 要将文件上传到facebook,您应该对“ name”“ tmp_name”感兴趣的值。

So the arguments that you should send to facebook for the photo upload should look something like this 因此,您应发送至Facebook进行照片上传的参数应如下所示:

$args = array( 'message' => 'Photo from application', ); $ args = array('message'=>'来自应用程序的照片',);

$args[$_FILES['file']['name']] = '@' . $ args [$ _ FILES ['file'] ['name']] ='@'。 $_FILES['file']['tmp_name']; $ _FILES ['file'] ['tmp_name'];

I think this should work for you. 我认为这应该为您工作。

Btw, i checked out the facebook doc for photo upload @ http://developers.facebook.com/docs/reference/api/photo they say the file name should be passed in the param "source", so if the above arguments dont work for you, you can try 顺便说一句,我签出了用于上传照片的facebook文档@ http://developers.facebook.com/docs/reference/api/photo他们说文件名应该在参数“源”中传递,因此如果上述参数不存在为您工作,您可以尝试

$args = array( 'message' => 'Photo from application', 'source' => '@' . $_FILES['file']['tmp_name'] ); $ args = array('message'=>'来自应用程序的照片','source'=>'@'。$ _FILES ['file'] ['tmp_name']);

Give it a try :) Hope this helps. 试试看:)希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM