使用codebird在PHP中使用Twitter API

时间:2015-11-06 02:34:13

标签: php twitter

我需要在我的项目中加入twitter功能。在所有库和包装器中,codebird似乎很方便。我尝试使用他们的示例中的代码进行基本身份验证,但在上传服务器上的文件后,我根本无法访问它们。它在服务器中显示错误500,我无法在localhost上测试它们。 index.php文件

<?php
 require_once ('codebird.php');
 \Codebird\Codebird::setConsumerKey('123456',    '1234567'); // static, see 'Using   multiple Codebird instances'

 $cb = \Codebird\Codebird::getInstance();

 session_start();

 if (! isset($_SESSION['oauth_token'])) {
     // get the request token
     $reply = $cb->oauth_requestToken([
    'oauth_callback' => 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']
]);

// store the token
$cb->setToken($reply->oauth_token, $reply->oauth_token_secret);
$_SESSION['oauth_token'] = $reply->oauth_token;
$_SESSION['oauth_token_secret'] = $reply->oauth_token_secret;
$_SESSION['oauth_verify'] = true;

// redirect to auth website
$auth_url = $cb->oauth_authorize();
header('Location: ' . $auth_url);
die();

 } elseif (isset($_GET['oauth_verifier']) && isset($_SESSION['oauth_verify'])) {
// verify the token
$cb->setToken($_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
unset($_SESSION['oauth_verify']);

// get the access token
$reply = $cb->oauth_accessToken([
    'oauth_verifier' => $_GET['oauth_verifier']
]);

// store the token (which is different from the request token!)
$_SESSION['oauth_token'] = $reply->oauth_token;
$_SESSION['oauth_token_secret'] = $reply->oauth_token_secret;

// send to same URL, without oauth GET parameters
header('Location: ' . basename(__FILE__));
die();
}

// assign access token on each page load
$cb->setToken($_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);

callback.php

<?php
 require_once ('codebird.php');
 \Codebird\Codebird::setConsumerKey('123456', '1234567'); // static, see 'Using multiple Codebird instances'

 $cb = \Codebird\Codebird::getInstance();

 if(isset($_SESSION['oauth_token'] &&    isset($_SESSION['oauth_token_secret']))){
     $cb->setToken($_SESSION['oauth_token'], $_SESSION['oauth_token_secret']); // see above

     $reply = (array) $cb->statuses_homeTimeline();
     print_r($reply);
 }

 else {
     echo 'necessary session variables couldnt be found!';
 }
 ?>

这可能是一个非常荒谬的问题,因为我只掌握PHP的基本知识,但是任何帮助都会非常适合。

0 个答案:

没有答案