dongwei6700 2014-04-10 12:58
浏览 66
已采纳

使用curl的Facebook Graph-API - 创建带图片的事件失败

I have tested the codes from this and this but it doesn't work.

The event is created successfully but the image is not published.

Here is my actual code:

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)."
".$url);
$r = curl_exec($ch);
curl_close($ch);
if($r!=''){
    $p = explode("

HTTP/", $r);
    $p = (count($p) > 1 ? 'HTTP/' : '') . array_pop($p);
    list($h, $b) = explode("

", $p, 2);
    return array('header' => $h, 'body' => $b);
}else{
    return array('header' => '', 'body' => 'error');
}
}
  • 写回答

2条回答 默认 最新

  • doujing1858 2014-04-10 13:31
    关注

    If you read the documentation, ther's no such field picture with \POST /{page-id}/events.

    So, while creating an event you can not publish a cover picture along with it. So it's a two-step procedure-

    1. Create event

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

      This you've already done, just delete the picture parameter from there.

    2. Upload cover picture

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

      Give the image to the parameter: source

    This way you can upload the cover pic to an event. Hope that helps.


    Edit

    The method to upload cover photo that I've mentioned will not work as of now (you can subscribe to this bug to get the update), instead you'll be needing a link to the image and make the call-

    \POST /{event-id}
    

    Parameter: cover_url

    This works. I've tested!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择