Post to a Facebook user's wall with cURL PHP
$attachment = array(
'access_token' => $token,
'message' => $msg,
'name' => $title,
'link' => $uri,
'description' => $desc,
'picture'=>$pic,
'actions' => json_encode(array('name' => $action_name,'link' => $action_link))
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/fbnameorid/feed');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to suppress the curl output
$result = curl_exec($ch);
curl_close ($ch);
How to post on user's wall in facebook after getting permission from user?
I've code to help you to post status with an image to an user's timeline.
After user has given permission to your app , you might have received a query string parameter called 'code' , using this code , we can post to user's timeline.
$code = @$_REQUEST["code"];
if(!empty($code))
{
// Start : Get live user access token ------------------------------------------ //
$token_url= "https://graph.facebook.com/oauth/"
. "access_token?"
. "client_id=" . FACEBOOK_APP_ID
. "&redirect_uri=" . urlencode( FACEBOOK_POST_LOGIN_URL)
. "&client_secret=" . FACEBOOK_APP_SECRECT
. "&code=" . $code;
$token = $this->get_app_access_token(FACEBOOK_APP_ID,FACEBOOK_APP_SECRECT,$token_url);
$access_token = $token;
// End : Get live user access token ------------------------------------------- //
// Start : Create album ------------------------------------------------------ //
$graph_url = "https://graph.facebook.com/me/albums?"
. "access_token=". $access_token;
$uri = 'https://graph.facebook.com/albums?access_token='.$access_token;
$post_fields = array('name' => trim( FACEBOOK_ALBUM_NAME ));
$curl = curl_init( $uri );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $curl, CURLOPT_POST, TRUE );
curl_setopt( $curl, CURLOPT_POSTFIELDS, $post_fields );
$raw_data = curl_exec($curl);
curl_close( $curl );
$created_album_id = json_decode( $raw_data, $assoc = TRUE );
// End : Create album ---------------------------------------------------------- //
$facebook_share_image_url = FACEBOOK_SHARE_IMAGE_PATH;
$facebook_status_text = 'The status text';
$graph_url= "https://graph.facebook.com/me/photos";
$postData = "url=" . urlencode($facebook_share_image_url)
. "&message=" . urlencode($facebook_status_text)
. "&access_token=" .$access_token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $graph_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$output = curl_exec($ch);
curl_close($ch);
// End : Add photos ------------------------------------------------------------- //
}
and the function to get app access token
function get_app_access_token($app_id, $secret,$token_url)
{
$url = 'https://graph.facebook.com/oauth/access_token';
$token_params = array(
"type" => "client_cred",
"client_id" => FACEBOOK_APP_ID,
"redirect_uri" => urlencode(FACEBOOK_POST_LOGIN_URL),
"client_secret" => FACEBOOK_APP_SECRECT
);
$a1 = $this->file_get_contents_curl($token_params,$token_url);
$a2 = str_replace("access_token=","",$a1);
$a3 = explode("&expires",$a2);
return $a3[0];
}
The other function access graph url
function file_get_contents_curl($params,$url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
$headers = array(
"Cache-Control: no-cache",
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}
Hope it helps..!!
Posting to facebook page wall via curl
To be able to post content you must grant publish_stream
permission to your application.
Requesting permissions can be done using OAuth Dialog.
BTW, If you just need it for user for testing purposes you may use Explorer Tool by choosing your application, clicking Get Access Token and checking needed permissions.
Update:
You will not be able to post on behalf of app, only user or page with appropriate access_token
(for page it will require manage_pages
and access_token
from accounts
connection of user
).
No need to specify perms
in access_token
retrieval URL. Permissions should be granted before that step.
PHP cURL : Post to Facebook Page as Page
If it gets posted as yourself, you are not using a Page Token. Debug your Token and see if the Page ID is listed - that´s how you know it´s a Page Token: https://developers.facebook.com/tools/debug/
Btw, you should use the new publish_pages
permission to post to Pages: https://developers.facebook.com/docs/apps/changelog#v2_3_changes
Information about how to get Access Tokens:
- https://developers.facebook.com/docs/facebook-login/access-tokens
- http://www.devils-heaven.com/facebook-access-tokens/
- http://www.devils-heaven.com/extended-page-access-tokens-curl/
Related Topics
Replace Woocommerce_Add_Order_Item_Meta Hook in Woocommerce 3.4
Use PHP Composer to Clone Git Repo
PHP APIs for Hotmail, Gmail and Yahoo
How to Send 500 Internal Server Error Error from a PHP Script
How to Run a PHP Without a Web Server
How to Create Installer for Website. PHP MySQL
Http_Build_Query with Same Name Parameters
How to Keep JSON_Encode() from Dropping Strings with Invalid Characters
Will Xpath 2.0 And/Or Xslt 2.0 Be Implemented in PHP
PHP Prepend Leading Zero Before Single Digit Number, On-The-Fly
What Is an .Inc and Why Use It
How to Solve Time Out in PHPmyadmin
PHP Gd Use One Image to Mask Another Image, Including Transparency
Post to a Facebook User's Wall with Curl PHP
How to Remove the Url Protocol and Slash from User Input in PHP