2010-06-22 63 views
7

可能重複:
Add picture at facebook event with Graph API附加圖片至Facebook事件(PHP SDK,休息或圖形API)

我一直在嘗試了好幾個小時,現在通過創建活動帶有圖像的Facebook API。到目前爲止,我已經能夠通過沒有圖像的Graph和Rest API創建事件,但一直無法附加任何圖像。

我相信REST API是唯一支持附加圖像的API,但文檔相當差,而php-sdk文檔或代碼也無濟於事。

我的最新嘗試是在上:http://promos.uk.glam.com/splash_test/example.php

隨着代碼:http://pastebin.com/8JC8RAck(注意 「要求facebook.php」 是PHP-SDK - http://github.com/facebook/php-sdk/

注線93(「如果($ uid){「),這應該是」if($ me){「,它工作了一個小時左右,然後停止工作(沒有改變填充$ me的代碼),奇怪。

因此,事件創建代碼是第96-99行,因爲它現在創建了一個沒有圖像的事件,但是一旦我放回註釋掉的代碼(第98行),它就無法執行任何操作。

有誰知道如何通過API在圖片上創建活動嗎?如果是這樣,請幫助我在這裏!如果有另一種解決方案,我沒有問題,但是我必須使用PHP或Javascript。

感謝所有

回答

10

感謝大家的回答,我決定重新檢視一下,看看API現在是否正常工作。它是!使用Graph API上傳圖片現在可以運行。感謝斯坦詹姆斯把我注意這一點,並給予代碼來發布照片,這是我改編的事件:

設置:

//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生氣!但它的工作。

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

這是你如何使用API​​將照片上傳到使用PHP的Facebook相冊。不是正好確定它也適用於事件。我會測試它,但我無法從工作中訪問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做到這一點,看到this answer