张贴在使用PHP 2011

问题描述:

没有被张贴到墙上用户的Facebook墙上,执行失控的尝试之后$结果= $ facebook-> API( '/ ME /饲料/', '后',$附件);声明,任何想法是什么破碎。张贴在使用PHP 2011

$facebook = new Facebook(array(
    'appId' => 'xxxxxxxxxxxx', 
    'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 
)); 

// Get User ID 


$user = $facebook->getUser(); 
if ($user) { 
    try { 
    // Get the user profile data you have permission to view 
    $user_profile = $facebook->api('/me'); 
    $uid = $facebook->getUser(); 


     $url = $facebook->getLoginUrl(array(
    'canvas' => 1, 
    'fbconnect' => 0, 
    'req_perms' => 'email,publish_stream,status_update,user_birthday,user_location,user_work_history')); 


    $attachment = array 
(
'access_token'=>$facebook->getAccessToken(), 
'message' => 'I had a question: should I write a PHP facebook app that actually worked?', 
'name' => 'I Asked Bert', 
'caption' => 'Bert replied:', 
'link' => 'http://apps.facebook.com/askbert/', 
'description' => 'NO', 
'picture' => 'http://www.facebookanswers.co.uk/img/misc/question.jpg' 
); 
echo "Test 1"; 

$result = $facebook->api('/me/feed/','post',$attachment); 

    echo "Test 2"; 
    $_SESSION['userID'] = $uid; 


    } catch (FacebookApiException $e) { 
    $user = null; 
    } 
} else { 
    die('Somethign Strange just happened <script>top.location.href="'.$facebook->getLoginUrl().'";</script>'); 
} 

测试1打印,但无法测试2.

+0

工作,你的错误的关键是:'用户已无权执行这种action'应用。看起来你还没有为用户启用facebook应用程序,或者没有设置用户发布到墙上的权限。 – Treffynnon

+0

@Treffynnon西隧SHD我做的就是它启用。如果你的意思是改变应用程序的迁移设置,我现在已经启用了它们。而另一点要注意的是错误并不在我的默认页面,上面的代码是其在base_facebook.php线970 – PUG

+0

[投递到Facebook墙 - PHP]的可能重复(http://facebook.*.com/questions/5577523 /后对Facebook的墙,PHP) – bkaid

下面的代码一定工作:即使你没有权限,它会尝试让他们

$facebook = new Facebook(array(
    'appId' => 'xxxxxxxxxxxxxxxxxx', 
    'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 
)); 
$user = $facebook->getUser(); 
$user_profile = $facebook->api('/me'); 


if(array_key_exists('publish_stream', $permissions['data'][0])) { 
       // Permission is granted! 
       // Do the related task 
       //$post_id = $facebook->api('/me/feed', 'post', array('message'=>'Hello World!')); 
        $post_id = $facebook->api('/me/feed', 'post', $attachment); 

      } else { 
       // We don't have the permission 
       // Alert the user or ask for the permission! 
       echo "Click Below to Enter!"; 
       header("Location: " . $facebook->getLoginUrl(array("scope" => "publish_stream"))); 
      } 

*警告

为2011年9月5日,这是工作的,但我看到在fb文档中,他们正在改变在用户墙上发布的方法,并且不鼓励使用发布流。但它现在

这里有两点要注意:

  1. 您正在使用新的PHP-SDK所以不要用req_perms使用scope代替
  2. 把你/me/feed后调用try
  3. 你并不需要调用getUser()两次,用户ID已经在$user
  4. 用户ID将是已经在会议上,与主要的,看起来像:fb_XXXXXXX_user_id其中XXXXXXX是你的应用程序ID
  5. 会议已经开始..
+0

的设置基本信息说,在这里http://facebookanswers.co.uk/?p=206说,对PHP 5中使用req_perms,并尝试范围虽然没有得到公布。感谢您对此提出的建议,并感到抱歉。 – PUG

+0

没有,如果你正确地阅读文章,新的PHP-SDK现在使用'scope'放慢参数 – ifaour

你说你正在寻找更新的文件,你检查Facebook PHP-SDK FAQ

具体来说,

  • 如何授权,并有下列权限?
  • 如何张贴在墙上?

后您创建一个应用程序实例让你$user第一

$user = $facebook->getUser(); 

从这里,从下面的说明“如何授权,并有下列权限?”使用范围

$par = array(); 
$par['scope'] = "publish_stream"; 

检查用户状态以查看哪个需要登录/登出方法使publish_stream许可

if ($user) { 
     $logoutUrl = $facebook->getLogoutUrl(); 
} else { 
     $loginUrl = $facebook->getLoginUrl($par); 
} 

然后把附着在解释“如何发布在墙壁上?”

if ($user) { 
$attachment = array('message' => 'this is my message', 
    'name' => 'This is my demo Facebook application!', 
    'caption' => "Caption of the Post", 
    'link' => 'http://mylink.com/ ', 
    'description' => 'this is a description', 
    'picture' => 'http://mysite.com/pic.gif ', 
    'actions' => array(array('name' => 'Get Search', 
    'link' => 'http://www.google.com/ ')) 
    ); 

    try { 
    // Proceed knowing you have a user who is logged in and authenticated 
    $result = $facebook->api('/me/feed/','post',$attachment); 
    } catch (FacebookApiException $e) { 
    error_log($e); 
    $user = null; 
    } 

} 

正如example app解释,放在一个try/catch块,看看有什么数据的可用性取决于进行API调用时,用户是否登录或没有。

一个调用,如

$cocacola = $facebook->api('/cocacola'); 

总是会因为它的工作是公开的。

+0

该异常来:1OAuthException:(#200)的用户没有授权的应用程序来执行此操作 – PUG

+0

@jam转到您的[应用程序](http://www.facebook.com/settings?tab=applications)并查看该应用程序授予的权限。 – phwd

+0

只有基本的,然后我改变了代码扩展需求 – PUG