将图片附加到Facebook活动(使用php sdk、rest或graph api)

7

可能是重复问题:
如何使用Graph API在Facebook事件中添加图片

我已经尝试了好几个小时,想通过Facebook API创建带有图片的事件。到目前为止,我已经能够通过Graph和Rest API创建没有图片的事件,但是无法附加任何图片。

我相信REST API是唯一支持附加图片的API,但文档非常差,php-sdk文档或代码也没有什么帮助。

我的最新尝试位于:http://promos.uk.glam.com/splash_test/example.php

使用的代码是:http://pastebin.com/8JC8RAck(请注意,“require facebook.php”是php-sdk - http://github.com/facebook/php-sdk/

请注意第93行(“if($ uid){”),这应该是“if($ me){”,它可以工作一个小时左右,然后停止工作(不更改填充$ me的代码),很奇怪。

因此,事件创建代码是第96-99行,现在它创建一个没有图片的事件,但只要我重新放回已注释的代码(第98行),它就无法执行任何操作。

有谁知道如何通过API在Facebook上创建带有图片的事件吗?如果是这样,请在这里帮助我!如果有其他解决方案,我没有问题舍弃所有这些代码,尽管我必须使用PHP或JavaScript。

谢谢大家

4个回答

10

感谢大家的答案,我决定重新审视这个问题,看看 API 是否现在正常工作。它确实可以了!使用 Graph API 现在可以上传图片。感谢 Stan James 引起我的注意并提供代码来发布照片,我为事件做了适应:

设置:

//Setup the Facebook object allowing file upload
$facebook = new Facebook(array(
  'appId'  => 'xxxxxxxxxxxxxxxxxxxx',
  'secret' => 'xxxxxxxxxxxxxxxxxxxx',
  'cookie' => true,
  'fileUpload' => true
));

帖子:

//Path to photo (only tested with relative path to same directory)
$file = "end300.jpg";
//The event information array (timestamps are "Facebook time"...)
$event_info = array(
    "privacy_type" => "SECRET",
    "name" => "Event Title",
    "host" => "Me",
    "start_time" => 1290790800,
    "end_time" => 1290799800,
    "location" => "London",
    "description" => "Event Description"
);
//The key part - The path to the file with the CURL syntax
$event_info[basename($file)] = '@' . realpath($file);
//Make the call and get back the event ID
$result = $facebook->api('me/events','post',$event_info);

我已经把所有的代码放在 pastebin 上了(http://pastebin.com/iV0JL2mL),它有点凌乱,因为我在几个月前调试时对 Facebook 很生气!但是它能运行。


2

这似乎有效

$file = "pj100.jpg";
$args = array(
    'access_token' => 'your access token',
    'name' => 'Event name',
    'description' => 'The Description ',
    'start_time' => '2012-09-01T12:00:00+0000',
    '@file.jpg' => '@' . realpath($file)); 
$target_url = "https://graph.facebook.com/me/events";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$result = curl_exec($ch);
curl_close($ch);

echo "Event ID= " . $result;

1
以下代码对我来说可行,使用PHP SDK
$facebook->setFileUploadSupport(true);
$attachment = array(
  'message' => $caption,
);    
$attachment[basename($file_path)] = '@' . realpath($file_path);
$result = $facebook->api('me/photos','post',$attachment);

若要使用原始的 curl 完成此操作,请参阅 此答案


0

您可以像这样在Facebook上发布图片:

// variables required in this example:
$img_path = "/home/www/test.png"; // image path
$img_caption = "testing"; // caption
$album_id = "12345678910"; // album id
$facebook_uid = "555555555"; // facebook user id
$facebok_user_access_token = "ajsdhflhasdf|sjdhflkasdf."; // facebook user token 

//upload photo
$args = array(
    'aid'       => $album_id,       // album id, possibly event id?
    'caption'   => $img_caption,    // caption for image
    'uid'       => $facebook_uid,   // facebook user id of the poster
    'format'    => 'json'
    );
$args[basename($img_path)] = '@' . realpath($img_path);
$ch = curl_init();
$url = "https://api.facebook.com/method/photos.upload?access_token={$facebook_user_access_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);
$upload_result = curl_exec($ch);

这是使用PHP上传照片到Facebook相册的API方法。不确定是否也适用于活动。我会测试一下,但我无法在工作中访问Facebook。
希望这是对你有所帮助的良好开端。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接