php – Facebook API:如何发布到自己的应用程序墙,没有登录

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – Facebook API:如何发布到自己的应用程序墙,没有登录脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我想发布到我自己的应用程序墙上一个脚本文本,但不先登录,因为它应该自动完成.我该怎么办?我试过了
$fb = new FaceBook(array(
    'appId'  => 'appid','secret' => 'appsecret','cookie' => true
));


if ($fb->getSession()) {
    // Post
} else {
    // LOGger
    // Every time I get in here :(
}

我需要做些什么才能使用脚本将其发布到我自己的应用程序墙上?

如果您想发布到自己的应用程序墙,所有您需要的是一个应用程序访问令牌,如果您想在没有登录的情况下发布到用户墙,则还需要此用户长期的实时访问令牌,因为您必须要求离线访问权限

要发布到您的应用程序墙:

1-卷曲此链接获取您的应用程序访问令牌:

https://graph.facebook.com/oauth/access_token
CLIENT_ID = YOUR_APP_ID&安培; client_secret = YOUR_APP_SECRET&安培;
grant_tyPE = client_credentials

2-发布到墙而不检查会话

示例:

<?PHP
require_once 'facebook.PHP'

//Function to Get Access Token
function get_app_token($appid,$appsecret)
{
$args = array(
'grant_type' => 'client_credentials','client_id' => $appid,'client_secret' => $appsecret
);

$ch = curl_init();
$url = 'https://graph.facebook.COM/oauth/access_token';
@R_777_223@($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HEADER,false);
curl_setopt($ch,CURLOPT_RETURNtransfer,true);
curl_setopt($ch,CURLOPT_POST,CURLOPT_POSTFIELDS,$args);
$data = curl_exec($ch);

return json_encode($data);
}

// Create FB Object Instance
$facebook = new Facebook(array(
    'appId'  => $appid,'secret' => $appsecret,'cookie' => false,));


//Get App Token
$token = get_app_token($appid,$appsecret);

//Try to Publish on wall or catch the Facebook exception
try {
$attachment = array('message' => '','access_token' => $token,'name' => 'Attachment Name','caption' => 'Attachment Caption','link' => 'http://apps.facebook.com/xxxxxx/','description' => 'Description .....','picture' => 'http://www.GOOGLE.com/logo.jpg','actions' => array(array('name' => 'Action Text','link' => 'http://apps.facebook.com/xxxxxx/'))
                    );

$result = $facebook->api('/'.$appid.'/Feed/','post',$attachment);
}

//If the post is not published,PRint error details
catch (FacebookApiException $e) {
echo '<pre>';
print_r($e);
echo '</pre>';
}

请查看本页面的APP LOgin部分以获取更多信息:
http://developers.facebook.com/docs/authentication/

脚本宝典总结

以上是脚本宝典为你收集整理的php – Facebook API:如何发布到自己的应用程序墙,没有登录全部内容,希望文章能够帮你解决php – Facebook API:如何发布到自己的应用程序墙,没有登录所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。