Updating Facebook Status from PHP

Here is the simple example about how to update Facebook status from PHP. We use different approach, just login to Facebook mobile and change the status. It’s simple and fun.

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
45
46
47
48
49
50
<?php
  // coder: Sony AK Knowledge Center - www.sony-ak.com
 
  // your facebook credentials
  $facebookLogin = "your_email_login_to_facebook";
  $facebookPassword = "your_facebook_password";
 
  // do login to facebook
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, "https://login.facebook.com/login.php?m&next=http://m.facebook.com/home.php");
  curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_POST, 1);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($curl, CURLOPT_POSTFIELDS, "email=" . $facebookLogin . "&pass=" . $facebookPassword . "&login=Log In");
  curl_setopt($curl, CURLOPT_ENCODING, "");
  curl_setopt($curl, CURLOPT_COOKIEJAR, getcwd() . '/cookies_facebook.cookie');
  $curl_data = curl_exec($curl);
  curl_close($curl);
 
  // do get post url
  $urlPost = substr($curl_data, strpos($curl_data, "action=") + 8);
  $urlPost = substr($urlPost, 0, strpos($urlPost, "\""));
  $urlPost = "http://m.facebook.com" . $urlPost;
 
  // do get some parameters for updating the status
  $fbDtsg = substr($curl_data, strpos($curl_data, "name=\"fb_dtsg\""));
  $fbDtsg = substr($fbDtsg, strpos($fbDtsg, "value=") + 7);
  $fbDtsg = substr($fbDtsg, 0, strpos($fbDtsg, "\""));
 
  $postFormId = substr($curl_data, strpos($curl_data, "name=\"post_form_id\""));
  $postFormId = substr($postFormId, strpos($postFormId, "value=") + 7);
  $postFormId = substr($postFormId, 0, strpos($postFormId, "\""));
 
  // do update facebook status
  $statusMessage = "Hey, I can update my facebook status from PHP"; // type your status message here :)
 
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, $urlPost);
  curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_POST, 1);
  curl_setopt($curl, CURLOPT_POSTFIELDS, "fb_dtsg=" . $fbDtsg . "&post_form_id=" . $postFormId . "&status=" . $statusMessage . "&update=Update Status");
  curl_setopt($curl, CURLOPT_ENCODING, "");
  curl_setopt($curl, CURLOPT_COOKIEFILE, getcwd() . '/cookies_facebook.cookie');
  curl_setopt($curl, CURLOPT_COOKIEJAR, getcwd() . '/cookies_facebook.cookie');
  $curl_data = curl_exec($curl);
  curl_close($curl);
 
  echo "Your Facebook status already updated with '" . $statusMessage . "'";
Bookmark and Share

Related posts:

  1. Update Status on LinkedIn using PHP
  2. Posting Shoutout Message to Friendster using PHP and curl

2 Comments

  1. mamet says:

    woww…..keren bgt nehh..thanks bgt ya bro’ ^_*
    oia salam kenal yah.

  2. Ndank says:

    Good tutorial, pak sony.Use curl to transfer and retrieve data from m.facebook.com.

Leave a Reply