透過 FB Graph API 取得資訊

edited 十二月 2013 in 原創軟體分享區
這是一個物件的兩個方法,用來在透過 Facebook API 登入後讀取需要的資訊

指定的需求是在某個 app 對應的 page 中發布一個廣告連結,要求使用者必須在指定的連結進行分享與回應來參與活動,其中 sharedposts 會取得存取者對於該連結進行分享的文章,而 comments 則是取得存取者對於該連結進行評論的內容

    public function good() {
$appId = 'xxx';
$linkId = 'yyy';
if (false === $this->query('xxx')) {
$this->redirect('/auth/facebook');
}
$this->set('email', $this->query('me?fields=email'));
$this->set('link', $this->query($linkId));
$this->set('sharedposts', $this->query($linkId . '?fields=sharedposts'));
$this->set('comments', $this->query($linkId . '?fields=comments'));
}

private function query($command = '') {
$fbSession = $this->Session->read('Facebook');
if (empty($fbSession['credentials']['token'])) {
return false;
}
$fbPrefix = 'https://graph.facebook.com/';
$fbSurfix = '&method=GET&format=json&access_token=' . $fbSession['credentials']['token'];
if (false === strpos($command, '?')) {
$command .= '?';
$fbSurfix = substr($fbSurfix, 1);
}
$now = time();
$tokenTime = strtotime($fbSession['credentials']['expires']);
if ($tokenTime > $now) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $fbPrefix . $command . $fbSurfix);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) {
curl_close($ch);
return false;
}
curl_close($ch);

if (!empty($result)) {
return json_decode($result);
}
}
return false;
}
標籤:

評論

Sign In or Register to comment.