2014-04-10 47 views
1

我測試了代碼thisthis 但它不起作用。Facebook Graph-API使用捲曲 - 創建事件與圖片失敗

事件已成功創建,但圖像未發佈。

這裏是我的實際代碼:

require_once '../admini/config.php'; 

$t = getToken('appID', 'appSecret'); 

$file = 'Koala.jpg'; 
$arrData = array(
    'name' => 'Test Event', 
    'start_time' => '2015-07-04', //ISO-8601 format - With Time -  2012-07-04T19:00:00-0700 
    //'end_time' => '', //optional 
    'description' => 'Das erste Test-Event', 
    'location' => 'Deggendorf', //Just a name 
    'location_id' => '103091806397289', //place id - inserts a link to place fb page 
    'ticket_url' => 'url', //URL to buy a ticket for the event 
    'no_feed_story' => FALSE, //TRUE = dont display on page 
    'access_token' => 'token', 
    'picture' => '@'. realpath($file), 
    ); 
$createUrl = 'https://graph.facebook.com/page_id/events'; 


$test = fbcurl($createUrl, 'POST', $arrData); //Returns the event id 

echo '<pre>'; 
print_r($test); 
echo '</pre>'; 

function fbcurl($url, $method, $fields = array(), $auth = array()){ 

foreach($fields as $key => $value){ 
    if(!is_string($value)){ 
     $fields[$key] = json_encode($value); 
    } 
} 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:multipart/form-data')); 
// curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); 
// if(count($auth)===2) 
//  curl_setopt($ch, CURLOPT_USERPWD, $auth['user'].':'.$auth['pass']); 
// } 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
if(count($fields)>0){ 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields, null, '&')); 

} 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
//curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); 
//curl_setopt($ch, CURLOPT_TIMEOUT, 60); 

//curl_setopt($ch, CURLOPT_USERAGENT, 'facebook-php-3.2'); 
//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "Content-Type: multipart/form-data"); 
    //file_put_contents('fbcurllocal.txt', print_r(http_build_query($fields, null,  '&'),  true)."\n".$url); 
$r = curl_exec($ch); 
curl_close($ch); 
if($r!=''){ 
    $p = explode("\r\n\r\nHTTP/", $r); 
    $p = (count($p) > 1 ? 'HTTP/' : '') . array_pop($p); 
    list($h, $b) = explode("\r\n\r\n", $p, 2); 
    return array('header' => $h, 'body' => $b); 
}else{ 
    return array('header' => '', 'body' => 'error'); 
} 
} 
+0

從哪裏呢'Koala.jpg'來的? –

+0

它保存在與php文件相同的文件夾中。 – nonsindeads

回答

0

如果你讀了documentation,療法沒有這樣的領域picture\POST /{page-id}/events

因此,創建活動時,您不能發佈封面圖片。所以這是一個兩步程序 -

  1. 創建活動

    API:\POST /{page-id}/events - Reference

    這個你已經做了,就從那裏刪除picture參數。

  2. 上傳封面圖片

    API:\POST /{event-id}/events - Reference

    給圖像的參數:source

這樣,您就可以上傳封面PIC到的事件。希望有所幫助。


編輯

的方法上傳,我已經提到不會爲現在的工作封面照片(你可以訂閱這個bug得到更新),而不是你會是需要一個鏈接到圖像,使呼叫 -

\POST /{event-id} 

參數:cover_url

This Works。我測試過了!

+0

不起作用!我測試了這個'圖片'=>'@'。realpath($ file)'; type = image/jpeg',bevore – nonsindeads

+0

請檢查編輯。我已經添加了正確的方法來做到這一點 –

+0

THX!但Facebook的味道像廢話! – nonsindeads

0

先看看通用之前瞭解它。

'asdf' => '@'. realpath($file).';type=image/jpeg', 

(
[header] => HTTP/1.1 200 OK 
x-fb-rev: 1200162 
Access-Control-Allow-Origin: * 
Cache-Control: private, no-cache, no-store, must-revalidate 
Content-Type: application/json; charset=UTF-8 
Date: Thu, 10 Apr 2014 14:26:20 GMT 
Expires: Sat, 01 Jan 2000 00:00:00 GMT 
Pragma: no-cache 
X-FB-Debug: nLKpXn4cbeF4AMa8kiOVMAfcKG5EFvQZmqvxFJXYC88= 
Connection: keep-alive 
Content-Length: 4 
[body] => true 
) 

我測試了這個

'source' => '@'. realpath($file).';type=image/jpeg', 

'source' => '@'. realpath($file), 

bevore