Access the inbox of Facebook Users with PHP

you might be interested to add facebook inbox manipulation functionality for your next app. I did the same few days back. Thought it could save a good amount of time for you! if you like it say it plz share it with others.

This class can be used to access the inbox of Facebook users.  It can use the Facebook Inbox API to perform several types of operations to access the inbox of a given Facebook user.  Currently it can get information about the mailbox folder, get information about the message threads, get the list of unread threads, search the inbox for a given keyword, retrieve a given message.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
 * @link                  http://dynamicguy.com
 
 * @since               Version 1.0
 
 * @license            LGPL
 
 */
 
class FBInbox {
 
    private $_uid; // Logged in user id
 
    public $facebook;
 
    /**
 
     * Constructor - Setup application credentials
 
     *
 
     * The constructor need to be called with the API key, API secret. It returns the logged in user id
 
     */
 
    public function __construct($apiKey, $apiSecret, $session = true) {
 
        $this->facebook = new Facebook($apiKey, $apiSecret, $session);
 
        $this->_uid = $this->facebook->require_login();
 
    }
 
    // --------------------------------------------------------------------
 
    /**
 
     * Get user's extended permission for reading inbox
 
     *
 
     * @access	private
 
     * @return	boolean
 
     */
 
    private  function _getExtendedPermission() {
 
        if (!$this->facebook->api_client->users_hasAppPermission("read_mailbox")) {
 
            echo 'Read Mailbox';
 
        }else {
 
            return true;
 
        }
 
    }
 
    // --------------------------------------------------------------------
 
    /**
 
     * Get user's folder informations
 
     *
 
     * @access	public
 
     * @return	Array
 
     */
 
    public function getFolderInfo() {
 
        if($this->_getExtendedPermission()) {
 
            $fql = "SELECT folder_id, name, unread_count, viewer_id FROM mailbox_folder WHERE viewer_id={$this->_uid}";
 
            $status = $this->facebook->api_client->fql_query($fql);
 
            return $status;
 
        }else {
 
            return false;
 
        }
 
    }
 
    // --------------------------------------------------------------------
 
    /**
 
     * Get user's extended permission for reading inbox
 
     * Currently, all users only have three Inbox folders: Inbox (folder_id = 0), Outbox (folder_id = 1), and Updates (folder_id = 4)
 
     * @access	public
 
     * @param $folderId Integer
 
     * @return	Array
 
     */
 
    public function getAllThreads($folderId = 0) {
 
        if($this->_getExtendedPermission()) {
 
            $fql = "SELECT thread_id, folder_id, subject, recipients, updated_time, parent_message_id, parent_thread_id, message_count, snippet, snippet_author, object_id, unread, viewer_id FROM thread WHERE folder_id = $folderId AND viewer_id={$this->_uid} LIMIT 20";
 
            $threads = $this->facebook->api_client->fql_query($fql);
 
            return $threads;
 
        }else {
 
            return false;
 
        }
 
    }
 
    // --------------------------------------------------------------------
 
    /**
 
     * Get user's extended permission for reading inbox
 
     * Currently, all users only have three Inbox folders: Inbox (folder_id = 0), Outbox (folder_id = 1), and Updates (folder_id = 4)
 
     * @access	public
 
     * @param $folderId Integer
 
     * @return	Array
 
     */
 
    public function getUnreadThreads($folderId = 0) {
 
        if($this->_getExtendedPermission()) {
 
            $fql = "SELECT thread_id, folder_id, subject, recipients, updated_time, parent_message_id, parent_thread_id, message_count, snippet, snippet_author, object_id, unread, viewer_id FROM thread WHERE folder_id = $folderId AND viewer_id={$this->_uid} AND unread != 0 LIMIT 20";
 
            $threads = $this->facebook->api_client->fql_query($fql);
 
            return $threads;
 
        }else {
 
            return false;
 
        }
 
    }
 
    // --------------------------------------------------------------------
 
    /**
 
     * Get user's extended permission for reading inbox
 
     * Currently, all users only have three Inbox folders: Inbox (folder_id = 0), Outbox (folder_id = 1), and Updates (folder_id = 4)
 
     *
 
     * @access	public
 
     * @param $key String
 
     * @param $folderId Integer
 
     * @return	Array
 
     */
 
    public function searchInbox($key, $folderId = 0) {
 
        if($this->_getExtendedPermission()) {
 
            $fql = "SELECT thread_id, folder_id, subject, recipients, updated_time, parent_message_id, parent_thread_id, message_count, snippet, snippet_author, object_id, unread, viewer_id FROM thread WHERE folder_id = $folderId AND viewer_id={$this->_uid} AND CONTAINS('$key') LIMIT 20";
 
            $threads = $this->facebook->api_client->fql_query($fql);
 
            return $threads;
 
        }else {
 
            return false;
 
        }
 
    }
 
    // --------------------------------------------------------------------
 
    /**
 
     * Get user's extended permission for reading inbox
 
     * Currently, all users only have three Inbox folders: Inbox (folder_id = 0), Outbox (folder_id = 1), and Updates (folder_id = 4)
 
     *
 
     * @access	public
 
     * @param $threadId Integer
 
     * @param $messageId Integer
 
     * @return	Array
 
     */
 
    public function readMessage($threadId, $messageId = NULL) {
 
        if($this->_getExtendedPermission()) {
 
            if(NULL == $messageId) {
 
                $fql = "SELECT message_id, thread_id, author_id, body, created_time, viewer_id FROM message WHERE thread_id = $threadId";
 
            }else {
 
                $fql = "SELECT message_id, thread_id, author_id, body, created_time, viewer_id FROM message WHERE thread_id = $threadId  AND message_id = $messageId";
 
            }
 
            $message = $this->facebook->api_client->fql_query($fql);
 
            return $message;
 
        }else {
 
            return false;
 
        }
 
    }
 
}

No related posts.

3 Responses to “Access the inbox of Facebook Users with PHP”

  1. I would appreciate any feedback here :)

  2. Exactly where is it, i’d like to read much more about this post, thank you.

  3. interesting post, pretty much covered it all for me, bookmarked.

Leave a Reply