Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
101 views
in Technique[技术] by (71.8m points)

publish_future_post in functions.php (wordpress) don't run script made with Facebook SDK 3

Dear Wordpress Hackers and Lovers,

I'm getting mad trying to let my script publishing a photo automatically to a fan page everytime a scheduled post is being published using the facebook SDK 3 (https://github.com/facebook/facebook-php-sdk).

The problem is that the script stop running when the wp_cron execute a line in functions.php that has to populate the object Facebook when published using the add_action('publish_future_post', 'future_post_being_published', 10, 1);. With publish_post works perfectly.

The script stop running when come across the following:

$facebook = new Facebook(array(
   'appId'  => '1xxxxxx1',
   'secret' => '28xxxxxxd91',
   'fileUpload' => true
  ));

This problem do not happen when the post is published directly from the backoffice, in fact the function add_action('publish_post', 'future_post_being_published', 10, 1); works perfectly.

I can say that it stop because I added some line to debug that send me an email in various part of the code but I get them all only when the publish_post is activated, and not with future_post_being_published.

Here the script:

function future_post_being_published($post_id)
{
$post = get_post($post_id);

wp_mail( 'test@test.com', 'TEST1', 'TEST1' );

  error_reporting(E_ALL);
  ini_set('display_errors', '1');
include '../../../script/facebook-php-sdk-master/src/facebook.php';

wp_mail( 'test@test.com', 'TEST2', 'TEST2' );

  $facebook = new Facebook(array(
   'appId'  => '1xxxx1',
   'secret' => '2xxxx1',
   'fileUpload' => true
  ));

wp_mail( 'test@test.com', 'TEST3', 'TEST3' );


  #It can be found at https://developers.facebook.com/tools/access_token/
  #Change to your token.
  $access_token = 'CxxxxxxxxxD';
  $params = array('access_token' => $access_token);
  #The id of the fanpage
  $fanpage = '3xxxxxxx9';
  #The id of the album
  $album_id ='4xxxxxxx37';

wp_mail( 'test@test.com', 'TEST4', 'TEST4' );


  $accounts = $facebook->api('/INSERT_USER_FACEBOOK/accounts', 'GET', $params);

wp_mail( 'test@test.com', 'TEST5', 'TEST5' );


  foreach($accounts['data'] as $account) {
     if( $account['id'] == $fanpage || $account['name'] == $fanpage ){
          $fanpage_token = $account['access_token'];
     }
  }

wp_mail( 'test@test.com', 'TEST6', 'TEST6' );


 $img = "http://www.test.com/test.jpg";
 $titolo ="Title!";

 $ret_obj = $facebook->api('/me/photos', 'POST', array(
    'url' => $img,
    'message' => $titolo . ' | Test !!!',
              'no_story' => 0,
          'access_token' => $fanpage_token,
          'aid' => $album_id
));     




wp_mail( 'test@test.com', 'TEST7', 'TEST7' );
}

add_action('publish_future_post', 'future_post_being_published', 10, 1);
add_action('publish_post', 'future_post_being_published', 10, 1);

So, to make it simple:

When it's invoked publish_post then the image is published in facebook page wall and i get all the emails (TEST1, TEST2, ..., TEST7) When it invoked publish_future_post then the image IS NOT published and I get only emails TEST1 and TEST2.

My question is: why the script doesn't publish the image when it's invoked publish_future_post? Maybe it cannot assign the values to the object facebook? How to fix it?

Thank you very much for the attention and for your precious help! Best Simone

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...