Codebird库代码在外部工作,但不在函数内部

时间:2017-05-17 14:32:47

标签: php wordpress api twitter codebird

我想创建一个函数,当我在WordPress网站上发布一些帖子时会发送自动推文。所以我在functions.php中包含了Codebird库,但是我在创建推文功能时遇到了问题。推文功能无效。

function post_to_twitter($message)
{
    $consumer_key = '....';
    $consumer_secret = '....';
    $access_token = '....';
    $access_secret = '....';

    \Codebird\Codebird::setConsumerKey($consumer_key, $consumer_secret);
    $cb = \Codebird\Codebird::getInstance();
    $cb->setToken($access_token, $access_secret);

    $params = array(
      'status' => $message
    );
    $reply = $cb->statuses_update($params);
}

$message = 'New post published';
post_to_twitter($message);

当我把它放在函数之外时,它就像这样

$consumer_key = '....';
$consumer_secret = '....';
$access_token = '....';
$access_secret = '....';
$message = 'New post published';

\Codebird\Codebird::setConsumerKey($consumer_key, $consumer_secret);
$cb = \Codebird\Codebird::getInstance();
$cb->setToken($access_token, $access_secret);

$params = array(
  'status' => $message
);
$reply = $cb->statuses_update($params);

0 个答案:

没有答案