Showing posts with label facebook. Show all posts
Showing posts with label facebook. Show all posts

Upload Photo with Facebook's Graph API

Posted by Unknown On Saturday, 22 December 2012 0 comments

publishing an Photo

In order to publish a photo to a user’s album, you must have the publish_stream permission. With that granted, you can upload a photo by issuing an HTTP POST request with the photo content and an optional description to one these to Graph API connections:

       
·           https://graph.facebook.com/USER_ID/photos - The photo will be published to an album created for your    app. We automatically create an album for your app if it does not already exist. All photos uploaded this way will then be added to this same album.
·         https://graph.facebook.com/ALBUM_ID/photos - The photo will be published to a specific, existing photo album, represented by the ALBUM_ID.
Sample code will be provided for the corresponding two scenarios:
1.      Uploading a photo to your app’s album.
2.      Creating a new album and uploading a photo to the album you create.


download

     Scenario 1: Uploading a photo to the app’s album

This is the scenario where you upload a photo to the USER_ID/photos Graph API endpoint. The user interface for this example allows the user to select a photo and add a caption before submitting the new photo. The newly created photo ID is returned to the user.
Using PHP:   
1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
       $app_id = "YOUR_APP_ID";
       $app_secret = "YOUR_APP_SECRET";
       $post_login_url = "YOUR_POST_LOGIN_URL";
    
       $code = $_REQUEST["code"];

       //Obtain the access_token with publish_stream permission 
       if(empty($code)){ 
          $dialog_url= "http://www.facebook.com/dialog/oauth?"
           . "client_id=" .  $app_id 
           . "&redirect_uri=" . urlencode( $post_login_url)
           .  "&scope=publish_stream";
          echo("<script>top.location.href='" . $dialog_url 
          . "'</script>");
         }
        else {
          $token_url="https://graph.facebook.com/oauth/access_token?"
           . "client_id=" . $app_id 
     . "&redirect_uri=" . urlencode( $post_login_url)
           . "&client_secret=" . $app_secret
           . "&code=" . $code;
          $response = file_get_contents($token_url);
          $params = null;
          parse_str($response, $params);
          $access_token = $params['access_token'];

         // Show photo upload form to user and post to the Graph URL
         $graph_url= "https://graph.facebook.com/me/photos?"
         . "access_token=" .$access_token;

         echo '<html><body>';
         echo '<form enctype="multipart/form-data" action="'
         .$graph_url .' "method="POST">';
         echo 'Please choose a photo: ';
         echo '<input name="source" type="file"><br/><br/>';
         echo 'Say something about this photo: ';
         echo '<input name="message" 
             type="text" value=""><br/><br/>';
         echo '<input type="submit" value="Upload"/><br/>';
         echo '</form>';
         echo '</body></html>';
      }
?>



READ MORE

Facebook Status updates using php SDK

Posted by Unknown On Friday, 21 December 2012 1 comments


It’s our new tutorial about Facebook status update using php SDK, you can update your Facebook status using simple PHP code and Facebook Graph API. This is the most naïve way to use this; you can extend this to make your own script to update your status from mobile.




download 



Ok let’s get started now! To be very frank, the status update script is already there in the SDK provided by Facebook, but very few people know. And there are people who won’t even know how easy it is.

Steps

1.      Create a Facebook app.

Login to Facebook and go to http://www.facebook.com/developers Click on the button at top right where it says “+ Set up New App”


Now let’s give a name to your app.





Next is to put the Canvas Page and Canvas URL. This Canvas Page will be used to Canvas URL is the location from where Facebook pulls all the content of your APP.




You will see your application’s unique App ID, API Key, App secret, keep it safe and we are going to use these in our example.



2.      Install Facebook php SDK.


You can download the latest version of SDK from https://github.com/facebook/php-sdk/and hit download.

 I have downloaded the latest version which is 2.1.2 till date.

We are going to use src folder which has the necessary code to interact with the Facebook API. Upload this folder to thehttp://yourdomain.com/fb-app-folder

download source code

                                                  



3. Create php status update code and add secret code and app id.

 Create your file which will update the status, call it as


 index.php .


<h1>Facebook Status Update</h1>
<?phpif ($_SERVER["REQUEST_METHOD"] == "POST") {
    
$status      $_POST['status'];
    
$facebook_id $userdata['id'];
    
$params      = array(
        
'access_token' => $access_token,
        
'message' => $status
    
);
    
$url         "https://graph.facebook.com/$facebook_id/feed";
    
$ch          curl_init();
    
curl_setopt_array($ch, array(
        
CURLOPT_URL => $url,
        
CURLOPT_POSTFIELDS => $params,
        
CURLOPT_RETURNTRANSFER => true,
        
CURLOPT_SSL_VERIFYPEER => false,
        
CURLOPT_VERBOSE => true
    
));
    
$result curl_exec($ch);
    
// End
    
echo "<span style='color:#cc0000'>Message Updated</span>";
}
?>
<form method="post" action="index.php">
<textarea name="status"></textarea> <br/>
<input type="submit" value=" Update "/>
</form>
READ MORE

Fashion