// Set the default API response $resp = array(); $resp['status'] = 'error'; $resp['type'] = 'unauthorized-access'; $resp['message'] = 'Unauthorized Access'; // Authenticate API Key if (empty($api_key) || !API::key_auth('popular', $api_key)) { die('Unauthorized Access'); } else { $resp['type'] = 'missing-parameters'; $resp['message'] = 'Warning: required parameters not found'; // Verify required parameters if (!empty($action) && !empty($post_id)) { switch ($action) { case 'like': $resp = Popular::add_page_like($post_id); break; default: $resp['type'] = 'invalid-action'; $resp['message'] = 'Defined API action cannot be performed'; break; } } // Redirect or return JSON response string if (!empty($redirect)) { $resp['message'] = base64_encode($resp['message']); header('Location: ' . $redirect . '?' . http_build_query($resp), TRUE, 303); } else { echo json_encode($resp); } }