【问题标题】:Facebook Graph-API using curl - Create event with picture failed使用 curl 的 Facebook Graph-API - 创建带有图片的事件失败
【发布时间】:2014-04-10 12:58:10
【问题描述】:

我已经测试了来自thisthis 的代码 但它不起作用。

活动创建成功,但图片未发布。

这是我的实际代码:

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

【问题讨论】:

  • Koala.jpg 是从哪里来的?
  • 它与php文件保存在同一文件夹中。

标签: php facebook facebook-graph-api curl facebook-php-sdk


【解决方案1】:

如果您阅读documentation,则没有picture\POST /{page-id}/events 这样的字段。

因此,在创建活动时,您不能同时发布封面图片。所以这是一个两步的过程-

  1. 创建活动

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

    这你已经完成了,只需从那里删除picture 参数。

  2. 上传封面图片

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

    把图片给参数:source

这样您就可以将封面图片上传到活动中。希望对您有所帮助。


编辑

我已经提到的上传封面照片的方法目前无法使用(您可以订阅此bug 以获取更新),您需要一个链接到图像并拨打电话-

\POST /{event-id}

参数:cover_url

这行得通。我已经测试过了!

【讨论】:

  • 不起作用!我已经测试了这张“图片”=>“@”。 realpath($file).';type=image/jpeg', 喜欢
  • 两步程序是正确的。 API 返回 TRUE,但事件没有任何变化...
  • 现在我已将“source”参数更改为“asdf”,它始终返回 true :-(
  • 我们遵循的方法是正确的,但似乎有一些 Facebook 错误。我发现以前的日期讨论相同,但还没有人能够解决它。任何更新都会通知您
  • 我和 facebook 的人沟通过,他们说文档很快就会更新,现在你只能使用链接更新封面图片 - 请在我的编辑中查看如何做到这一点跨度>
【解决方案2】:

看之前的评论就明白了。

'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),

请注意

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    • 2012-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多