Note that there are some explanatory texts on larger screens.

plurals
  1. POFacebook PHP SDK: Post object / action
    text
    copied!<p>I was wondering if anyone could help me.</p> <p>I'm trying to use the PHP SDK to post actions, but it doesn't seem to return an object after the request is made, nor does it update the activity section of the users profile with the action. I had this working fine in JS but can't seem to move it to php.</p> <pre><code>// include the FB SDK require_once(APPLICATION_PATH . '/../library/facebook/facebook.php'); // create the application instance $facebook = new Facebook(array( 'appId' =&gt; $this-&gt;facebookConfig-&gt;appid, 'secret' =&gt; $this-&gt;facebookConfig-&gt;appsecret, 'cookie' =&gt; true )); // get the user id $user = $facebook-&gt;getUser(); if(!empty($user)) { // We have a user ID, so probably a logged in user. // If not, we'll get an exception, which we handle below. try { // post og actions if(!empty($this-&gt;thanksData-&gt;ogobject) &amp;&amp; !empty($this-&gt;thanksData-&gt;ogaction)){ $method = '/me/' . $this-&gt;appData-&gt;namespace . ':' . $this-&gt;thanksData-&gt;ogaction; $params = array( $this-&gt;thanksData-&gt;ogobject =&gt; 'http://mydomain.com/thanks', //'access_token' =&gt; $this-&gt;fbAuthToken ); $ret_obj = $facebook-&gt;api($method, 'POST', $params); } // wall post if(!empty($this-&gt;facebookConfig-&gt;wallmsg)) $this-&gt;wallPost($facebook); } catch(FacebookApiException $e) { // If the user is logged out, you can have a // user ID even though the access token is invalid. // In this case, we'll get an exception, so we'll // just ask the user to login again here. $params = array( 'scope' =&gt; $this-&gt;permissions, 'redirect_uri' =&gt; $this-&gt;facebookConfig-&gt;applink ); $loginUrl = $facebook-&gt;getLoginUrl($params); // log errors error_log($e-&gt;getType()); error_log($e-&gt;getMessage()); // redirect if not logged in or not enough permissions //echo "&lt;script&gt;top.location=\"".$loginUrl."\";&lt;/script&gt;";die; } // Give the user a logout link //echo '&lt;br /&gt;&lt;a href="' . $facebook-&gt;getLogoutUrl() . '"&gt;logout&lt;/a&gt;'; } </code></pre> <p><strong>Example of the same in JS</strong> This code was working before I removed it in favour of doing the task in PHP</p> <pre><code>// initialise facebook window.fbAsyncInit = function() { // get the data var data = $('body').data(); FB.init({ appId : data.appid, // App ID status : true, // check login status cookie : true, // enable cookies to allow the server to access the session xfbml : true, // parse XFBML oauth : true // oauth stuff }); // enable actions on page visit if(data.object.length &gt; 0 &amp;&amp; data.action.length &gt; 0){ // check if we have permissions FB.login(function(loginResponse){ FB.api('me/permissions','get',function(response){ // get the data var data = $('body').data(); if(data.ogactions == 1){ var params = {}; params[data.object] = data.url; // post that we read the article FB.api( '/me/'+ data.appnamespace + ':' + data.action, 'post', params, function(response) { if (!response || response.error) { console.log(response); } else { console.log('Action successful! Action ID: ' + response.id); } }); } }); }, {scope: 'publish_actions,publish_stream'}); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload