alexpyoung
alexpyoung

Reputation: 335

Creating user-owned Open Graph objects with Facebook's PHP SDK v4

Has anyone tried creating user-owned Facebook Open Graph objects using v4.0.* of their PHP SDK? I wasn't able to find any working examples of an up-to-date Open Graph PHP request online.

Currently, my request looks like so:

$graphObject = (
    new FacebookRequest(
        $this->facebookSession, 
        'POST', 
        "/me/objects/{$objectType}", [
            'title' => $title,
            'image' => $image,
            'url' => $url,
            'description' => $description,
            'site_name' => $this->siteName
        ]
    )
)->execute()->getGraphObject();

Which throws a FacebookAuthorizationException:

(#100) The parameter object is required

I have also tried this request:

$graphObject = (
    new FacebookRequest(
        $this->facebookSession, 
        'POST', 
        "/me/objects/{$objectType}", [
            'title' => $title,
            'image' => $image,
            'url' => $url,
            'description' => $description,
            'type' => $objectType,
            'site_name' => $this->siteName
        ]
    )
)->execute()->getGraphObject();

Which throws a FacebookAuthorizationException:

Cannot specify type in both the path and query parameter

Lastly, I have tried this request:

$graphObject = (
    new FacebookRequest(
        $this->facebookSession, 
        'POST', 
        "/me/objects/", [
            'title' => $title,
            'image' => $image,
            'url' => $url,
            'description' => $description,
            'type' => $objectType,
            'site_name' => $this->siteName
        ]
    )
)->execute()->getGraphObject();

Which throws a FacebookAuthorizationException:

(#100) The parameter object is required

In the documentation for creating Open Graph objects I linked above, I see what the proper cURL request is, I'm simply a little lost on how to actually make this request using the PHP SDK. Thanks in advance for your help!

Upvotes: 2

Views: 198

Answers (1)

daviddoran
daviddoran

Reputation: 528

I believe you need to JSON encode the object parameter like so:

$graphObject = (
    new FacebookRequest(
        $this->facebookSession, 
        'POST', 
        "/me/objects/{$objectType}",
        [
          'object' => json_encode([
            'title' => $title,
            'image' => $image,
            'url' => $url,
            'description' => $description,
            'site_name' => $this->siteName
          ])
        ]
    )
)->execute()->getGraphObject();

Upvotes: 5

Related Questions