Software & Finance





Facebook Apps - How to post on friend's wall using Graph API





First you need to have PHP SDK to start with. You can download the PHP SDK for facebook at the following location:

https://github.com/facebook/php-sdk

 

 

 

Here is the sample code for Facebook Apps on how to post on Friend's wall using Graph API

 

    
    include_once 'facebook.php';

	$facebook = new Facebook(array(
		'appId' => 'YOUR_APP_ID',
		'secret' => 'YOUR_SECRET_KEY',
		'cookie' => true,
		'domain' => 'www.softwareandfinance.com'
	));

	$app_name = "Broadcast to Friends";
	$app_url = "http://apps.facebook.com/your_app_name/";

	$user_id = $facebook->getUser();

	if (!$user_id) {	
		$url = $facebook->getLoginUrl(array(
		'canvas' => 1,
		'fbconnect' => 0
		
		));

	} else 
	{
		try {
			$permissions = $facebook->api("/me/permissions");
			if(! array_key_exists('publish_stream', $permissions['data'][0]) ) { 
				header( "Location: " . $facebook->getLoginUrl(array("scope" => "publish_stream")) );
			} 

			$permissions = $facebook->api("/me/permissions");
	
			if( array_key_exists('publish_stream', $permissions['data'][0]) ) {
				$friends = array();
				$_friends = $facebook->api("/me/friends"); 
	
				if (is_array($_friends) && count($_friends)) {
					foreach ($_friends as $friend) { 
						$friends[] = $friend['uid'];
						$location = "/" . friend[uid] . "/feed";
						$post_id = $facebook->api($location, 'post', array('message'=>'Hello World!'));
						echo "posted on friend's wall" . $post_id;
					}
				}
			}

		} 
		catch (FacebookApiException $e) 
		{
			echo "Error: Unable to post on your friend's wall";

		}
	}