开发者

Managing Facebook Pages using PHP

开发者 https://www.devze.com 2023-03-01 22:02 出处:网络
I\'m looking to integrate my current website more deeply with Facebook using a Fan Page (notice: I did noticed the Open Graph protocol that Facebook support but i\'m looking to create a website-wide f

I'm looking to integrate my current website more deeply with Facebook using a Fan Page (notice: I did noticed the Open Graph protocol that Facebook support but i'm looking to create a website-wide fan page).

I know that you can create and manage Fan Pages using Facebook, but I'm looking for a way to do that using a PHP script - for example, post to the Fan Page wall, create events using the Fan Page and ideally - create secondary Fan Pages on the fly.

After looking around 开发者_如何学Cin Facebook's developer section I didn't find a method to do those tasks from outside using the Facebook API.

So my question is: how would you accomplish that?

Thanks!


Publishing posts to your Page, and creating Events, are both relatively trivial tasks. You can use the Graph API to do that.

Check out the area about Publishing specifically. It gives you the general overview of how publishing works, and this can be applied all over the Graph.

Also, the documentation about the Events portion of the Graph API has an example cURL post for how to create a new event via the Graph API.

Posting anything to your Facebook page is going to require you have the manage_pages extended permission, and it's probably a good idea to get the offline_access permission also.

An example of both posting to your Page wall and creating an event (in php) would look a lot like this:

<?php

require 'facebook.php';

$fb = new Facebook(array(
    'appId' => FB_APP_ID,
    'secret' => FB_APP_SECRET,
    'cookie' => true
));

$your_page_id = '123123123';

//get the access token to post to your page via the graph api
$accounts = $fb->api("/me/accounts");
foreach ($accounts['data'] as $account)
{
    if ($account['id'] == $your_page_id)
    {
        //found the access token, now we can break out of the loop
        $page_access_token = $account['access_token'];
        break;
    }
}

try
{
    //publish a story to the page's wall (as the page)
    $post_id = $fb->api("/{$your_page_id}/feed", "POST", array(
        'message' => "Hello to all my fans, I love you!"
        'access_token'  => $page_access_token;
    ));

    echo "Post published. ID: {$post_id}<br>";

    //create a new event.
    $event_id = $fb->api("/{$your_page_id}/events", "POST", array(
        "name"  => "My Totally Awesome Event, You Better Show UP!",
        "start_time" => time(), //it starts now...duh!
        "location"  => "Anywhere, USA"
    ));

    echo echo "Event created. ID: {$event_id}<br>";
}
catch (Exception $e)
{
    var_dump($e);
}

As for creating Pages on the fly, the only way you can do that is by using the Open Graph Protocol. The only restriction here is that pages have to have unique URLS. So you can assign each of your Open Graph objects a unique ID, and give them a URL like http://www.mysite.com/pages?id=123456. This will let you output the Open Graph tags required to generate the page on FB. You can then use the Graph API to get the ID of the Open Graph object after someone likes it like so: http://graph.facebook.com/?ids=http://www.mysite.com/pages?id=123456.

You can publish to these Open Graph objects the same exact way you'd publish to a standard Facebook Page.

Hope that helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号