Skip to content

Instantly share code, notes, and snippets.

@httpspace
Last active September 7, 2016 06:58
Show Gist options
  • Save httpspace/9ba4120bb3e880b3f860c28b5da635e5 to your computer and use it in GitHub Desktop.
Save httpspace/9ba4120bb3e880b3f860c28b5da635e5 to your computer and use it in GitHub Desktop.
fb tool
<?php
class ParserPages {
public static $app_token = '124546464634646|3d8f4e8993a8abfddfdscxvcxve5';
public static $max_times = 5;
public static function api($ins){
$token = self::$app_token;
$link = (strpos($ins, '?') !== false) ? '&' : '?';
$url = "https://graph.facebook.com/{$ins}{$link}access_token={$token}";
// echo $url;
$res = self::httpGet($url, 1);
return $res;
}
//取得網址按讚分享討論
public static function get_url_info($urls = []){
$urls = (gettype($urls) == 'array') ? implode(',', $urls) : $urls;
return self::api("?fields=og_object{likes.limit(0).summary(1)},share&ids={$urls}");
}
public static function exchange_token($token){
$config = explode('|', self::$app_token);
$id = $config[0];
$secret = $config[1];
$url = "https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id={$id}&client_secret={$secret}&fb_exchange_token={$token}";
$res = self::httpGet($url);
if($res){
parse_str($res, $output);
return $output;
}
return false;
}
//url 取得 fb object id
public static function get_id_by_url($url){
$token = self::$app_token;
// $token = '';
$url = "https://graph.facebook.com/{$url}?fields=likes,name,id&access_token={$token}";
// echo $url;
$res = self::httpGet($url);
// show($res);
return $res;
}
public static function set_max_times($num){
self::$max_times = $num;
}
// parser message by id
public static function parser($fb_id, $method = 'posts', $callback = null, $attr = false){
$def_attr = [
"until" =>false,
"since" =>false,
"fields"=>false
];
$fields = [
'full_picture',
'message',
'created_time',
'type',
'shares',
'likes.limit(1).summary(true)',
'comments.limit(1).summary(true)',
'object_id'
];
$token = self::$app_token;
$times = 0;
if(false == $attr) $attr = $def_attr;
$attr['fields'] =( false == $attr['fields'] )? $fields : $attr['fields'];
if(is_array($attr['fields']) ) $attr['fields'] = implode(',', $attr['fields']);
$fb_get_value = "access_token={$token}&";
//$attr['access_token'] = $token;
ksort($attr);
foreach ($attr AS $key => $value){
if(false == $value) continue;
$fb_get_value .= "&{$key}={$value}";
}
$url = "https://graph.facebook.com/{$fb_id}/{$method}?{$fb_get_value}";
// echo $url;
while(true){
$times++;
$stop_now = false;
$stop = false;
// echo $url.'<br/>';
$res = [];
$res = self::file_get_content($url, 1);
// show($times);
// echo '<br />';
// show(self::$max_times);
// echo '-----';
if(!$res) break;
if(is_callable($callback)){
if(isset($res['paging'], $res['paging']['next'])){
$url = $res['paging']['next'];
} else{
$stop_now = true;
}
// echo $url;
$stop = $callback($res);
// show($stop);
$stop = ($stop === false) ? false : true;
// show($stop);
//使用者中止或到達最大次數中止
if(!$stop || $stop_now || $times > self::$max_times) break;
}
}
}
public static function get_likes_info($id, $method = "feed"){
if($method == 'feed'){
$api_in = "{$id}?fields=shares,likes.limit(1).summary(true),comments.limit(1).summary(true)";
}
if($method == 'photos'){
$api_in = "{$id}?fields=shares,likes.limit(1).summary(true),comments.limit(1).summary(true)";
}
if($method == 'videos'){
$api_in = "{$id}?fields=likes.limit(1).summary(true),comments.limit(1).summary(true)";
}
$res = self::api($api_in);
// show($res);
if(!isset($res['error'])){
return array(
'shares' => isset($res['shares']) ? $res['shares']['count'] : 0,
'likes' => isset($res['likes']) ? $res['likes']['summary']['total_count'] : 0,
'comments' => isset($res['comments']) ? $res['comments']['summary']['total_count'] : 0
);
}
return false;
}
public static function get_reach($id){
$res = self::api($id.'/Insights/post_impressions_unique?fields=values&limit=1', 1);
// show($res );
return isset($res['error']) ? false :
isset($res['data'][0], $res['data'][0]['values']) ? ['reach' => $res['data'][0]['values'][0]['value']] : ['reach' => 0];
}
//http get
public static function httpGet($url, $json = false){
$ch = curl_init($url);
curl_setopt( $ch, CURLOPT_POST, false );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
curl_setopt( $ch, CURLOPT_HEADER, false );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$data = curl_exec( $ch );
return ($json) ? json_decode($data, 1) : $data;
}
public static function file_get_content($url, $json = false){
try {
$data = file_get_contents($url);
return ($json) ? json_decode($data, 1) : $data;
} catch (Exception $e) {
return false;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment