|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace databoxtech\multisocial\adapter; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use databoxtech\multisocial\ApplyConfig; |
|
8
|
|
|
use databoxtech\multisocial\Attachment; |
|
9
|
|
|
use databoxtech\multisocial\exception\SocialException; |
|
10
|
|
|
use Facebook\Exceptions\FacebookSDKException; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class FacebookAdapter : facebook adapater for multi social library |
|
14
|
|
|
* @package databoxtech\multisocial\Adapter |
|
15
|
|
|
* |
|
16
|
|
|
* Ref: https://developers.facebook.com/docs/graph-api/photo-uploads/ |
|
17
|
|
|
* |
|
18
|
|
|
*/ |
|
19
|
|
|
class FacebookAdapter implements SocialAdapter |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
use ApplyConfig; |
|
23
|
|
|
|
|
24
|
|
|
private $app_id = ''; |
|
25
|
|
|
private $app_secret = ''; |
|
26
|
|
|
private $access_token = ''; |
|
27
|
|
|
private $page_id = ''; |
|
28
|
|
|
private $default_graph_version = 'v2.10'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var \Facebook\Facebook |
|
32
|
|
|
*/ |
|
33
|
|
|
private $client; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @inheritDoc |
|
37
|
|
|
*/ |
|
38
|
2 |
|
public function __construct($config) |
|
39
|
|
|
{ |
|
40
|
2 |
|
$this->apply_config($config, ['app_id', 'app_secret', 'access_token', 'page_id']); |
|
41
|
|
|
try{ |
|
42
|
2 |
|
$this->client = new \Facebook\Facebook([ |
|
43
|
2 |
|
'app_id' => $this->app_id, |
|
44
|
2 |
|
'app_secret' => $this->app_secret, |
|
45
|
2 |
|
'default_graph_version' => $this->default_graph_version, |
|
46
|
|
|
]); |
|
47
|
|
|
}catch (FacebookSDKException $ex){ |
|
48
|
|
|
throw new SocialException(self::class, $ex->getMessage()); |
|
49
|
|
|
} |
|
50
|
2 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @inheritDoc |
|
54
|
|
|
*/ |
|
55
|
|
|
public function post($post) |
|
56
|
|
|
{ |
|
57
|
|
|
$data = [ |
|
58
|
|
|
'caption' => $post->getCaption(), |
|
59
|
|
|
'message' => $post->getDescription(), |
|
60
|
|
|
]; |
|
61
|
|
|
$a = 0; |
|
62
|
|
|
if(count($post->getAttachments()) > 0){ |
|
63
|
|
|
try{ |
|
64
|
|
|
$data["attached_media[{$a}]"] = $this->upload_attachments($post->getAttachments()); |
|
65
|
|
|
}catch (FacebookSDKException $ex){ |
|
66
|
|
|
throw new SocialException(self::class, $ex->getMessage()); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
try{ |
|
71
|
|
|
$response = $this->client->post("{$this->page_id}/feed", $data, $this->access_token); |
|
72
|
|
|
$id = $response->getGraphNode()->getField('id', null); |
|
73
|
|
|
}catch (FacebookSDKException $ex){ |
|
74
|
|
|
throw new SocialException(self::class, $ex->getMessage()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if($id == null){ |
|
78
|
|
|
throw new SocialException(self::class, 'Error creating post'); |
|
79
|
|
|
} |
|
80
|
|
|
return $id; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param Attachment[] $attachments |
|
85
|
|
|
* @return array |
|
86
|
|
|
* @throws FacebookSDKException |
|
87
|
|
|
* @throws SocialException |
|
88
|
|
|
*/ |
|
89
|
|
|
private function upload_attachments(array $attachments){ |
|
90
|
|
|
$ids = []; |
|
91
|
|
|
foreach ($attachments as $attachment){ |
|
92
|
|
|
$endpoint = "{$this->page_id}/photos"; |
|
93
|
|
|
if($attachment->getType() == Attachment::TYPE_VIDEO){ |
|
94
|
|
|
$endpoint = "{$this->page_id}/videos"; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$response = $this->client->post($endpoint, [ |
|
98
|
|
|
'published' => false, |
|
99
|
|
|
'caption' => $attachment->getCaption(), |
|
100
|
|
|
'source' => $this->client->fileToUpload($attachment->getPath()) |
|
101
|
|
|
], $this->access_token); |
|
102
|
|
|
$id = $response->getGraphNode()->getField('id', null); |
|
103
|
|
|
if($id == null){ |
|
104
|
|
|
throw new SocialException(self::class, 'Error uploading file'); |
|
105
|
|
|
} |
|
106
|
|
|
$ids[] = $id; |
|
107
|
|
|
} |
|
108
|
|
|
return $ids; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
} |