Skip to content

Instantly share code, notes, and snippets.

View niraj-shah's full-sized avatar
🏠
Working from home

Niraj Shah niraj-shah

🏠
Working from home
View GitHub Profile
@niraj-shah
niraj-shah / fb_php_sdk_4.1_full.php
Created February 19, 2015 18:06
Getting started with the Facebook PHP SDK v4.1. The complete code.
<?php
// Set the location of the Facebook PHP SDK - this should point to directory containing autoload.php
define( 'FACEBOOK_SDK_V4_SRC_DIR', './Facebook/' );
// include required files from Facebook SDK
require_once( 'Facebook/autoload.php' );
// start the session
session_start();
@niraj-shah
niraj-shah / fb_php_sdk_4.1c.md
Last active August 29, 2015 14:15
Code explanation for fb_php_sdk_4.1c.php

Line 4: We start by using the RedirectLoginHelper (you may be familiar with this from v4.0.x of the SDK). This is needed to generate the login / logout URLs and retrieve the Access Token from Facebook.

Lines 7-20: We check to see if the code has already been provided by Facebook after a login redirect. If we have one, we attempt to get the access_token using the helper (line 12), and then store this in the session (line 15).

Line 23: If we already have an access_token saved in the session we retrieve it here. The access_token saved on line 15 is also retrieved here. If the token does not exist, the $access_token variable will be null.

Line 26: We check if we have an access_token, and then make sure it hasn't expired.

Line 29: This is a function to set the default access_token for the user, so that all subsequent API calls use that token. If you don't want to set the default token, you can also pass it to requests manually.

@niraj-shah
niraj-shah / fb_php_sdk_4.1c.php
Created February 19, 2015 17:36
Getting started with the Facebook PHP SDK v4.1. Part c: Login and make a API call
<?php
// login helper with redirect_uri
$helper = $fb->getRedirectLoginHelper();
// see if we have a code in the URL
if ( isset( $_GET['code'] ) ) {
// get new access token if we've been redirected from login page
try {
@niraj-shah
niraj-shah / fb_php_sdk_4.1b.php
Created February 19, 2015 17:27
Getting started with the Facebook PHP SDK v4.1. Part b: Setting up the application.
<?php
// setup application using API keys and handlers
$fb = new Facebook\Facebook([
'app_id' => 'xxx',
'app_secret' => 'yyy',
'http_client_handler' => 'curl', // can be changed to stream or guzzle
'persistent_data_handler' => 'session' // make sure session has started
]);
@niraj-shah
niraj-shah / fb_php_sdk_4.1.php
Created February 19, 2015 17:22
Getting started with the Facebook PHP SDK v4.1
<?php
// Set the location of the Facebook PHP SDK - this should point to directory containing autoload.php
define( 'FACEBOOK_SDK_V4_SRC_DIR', './Facebook/' );
// include required files from Facebook SDK
require_once( 'Facebook/autoload.php' );
// start the session
session_start();
@niraj-shah
niraj-shah / parse_file_upload.php
Created December 30, 2014 11:15
How to upload a file to Parse using the Official Parse PHP SDK
<?php
// define location of Parse PHP SDK, e.g. location in "Parse" folder
// Defaults to ./Parse/ folder. Add trailing slash
define( 'PARSE_SDK_DIR', './Parse/' );
// include Parse SDK autoloader
require_once( 'autoload.php' );
// Add the "use" declarations where you'll be using the classes
use Parse\ParseClient;
@niraj-shah
niraj-shah / rsync.sh
Last active August 29, 2015 14:09
Rsync from remote server to local
# remote to local
sudo rsync -uvzr -e 'ssh -p 2222 -l username' domain.com:/var/www/html/ /var/www/html
# local to remote
sudo rsync -uvzr /var/www/html/ -e 'ssh -p 2222 -l username' domain.com:/var/www/html
@niraj-shah
niraj-shah / status_bar.html
Created November 17, 2014 21:46
Meta Data to change the Status Bar on devices running Android 5.0 Lollipop
<html>
<head>
<meta name="theme-color" content="#53A6DC">
</head>
<body>
</body>
</html>
@niraj-shah
niraj-shah / logout.php
Created October 27, 2014 11:31
Logout of Facebook PHP SDK 4.0.x session
<?php
// start session
session_start();
// kill the session
session_destroy();
// redirect back to website home
header( 'Location: http://yourwebsite.com/login' );
@niraj-shah
niraj-shah / page_post.php
Last active May 16, 2023 10:32
Posting to a Page using Facebook PHP SDK v4.0.x and Graph API v2.x
<?php
// post to page
$page_post = (new FacebookRequest( $session, 'POST', '/'. $page_id .'/feed', array(
'access_token' => $access_token,
'name' => 'Facebook API: Posting As A Page using Graph API v2.x and PHP SDK 4.0.x',
'link' => 'https://www.webniraj.com/2014/08/23/facebook-api-posting-as-a-page-using-graph-api-v2-x-and-php-sdk-4-0-x/',
'caption' => 'The Facebook API lets you post to Pages you administrate via the API. This tutorial shows you how to achieve this using the Facebook PHP SDK v4.0.x and Graph API 2.x.',
'message' => 'Check out my new blog post!',
) ))->execute()->getGraphObject()->asArray();