$token, 'ids' => implode(',', $ids), 'method' => 'GET', ); $profiles = FacebookMethods::fetchUrl($url, $graph_params); return $profiles; } function get_insights($token, $id, $start) { $start->modify('+1 day'); $path = $id . '/insights'; $url = FacebookMethods::getGraphApiUrl($path); $params = array( 'access_token' => $token, 'method' => 'GET', 'since' => $start->format('Y-m-d'), ); $start->modify('+1 day'); $params['until'] = $start->format('Y-m-d'); $insights = FacebookMethods::fetchUrl($url, $params); return $insights; } $token = $_REQUEST['access_token']; $start = new DateTime($_REQUEST['date']); if (isset($_REQUEST['id'])) { $id = $_REQUEST['id']; $insights = get_insights($token, $id, $start); header('Content-Type: text/plain'); echo "object_id,metric,end_time,period,value\n"; foreach ($insights['data'] as $metric) { foreach ($metric['values'] as $row) { $split = explode('/', $metric['id']); $date_str = explode('T', $row['end_time']); $date = new DateTime($date_str[0]); $date->modify('-1 day'); $value = $row['value']; if (is_array($row['value'])) $value = implode(' ', $row['value']); echo "{$split[0]},{$metric['name']},{$date->format('Y-m-d')}," . "{$metric['period']},{$value}\n"; } } } else { $profiles = get_index($token); // Render the index. ?>