0

Trying to connect to the twitter api with php. Using the files from the following github distribution: https://github.com/abraham/twitteroauth. I just get a 500 server error and have tried using error_reporting(E_ALL) but to no avail. Not sure what the rub is, here is my code:

<?php

  session_start();

  require_once("twitteroauth-master/src/TwitterOAuth.php");

  $apikey="not included but definitely correct";
  $apisecret="not included but definitely correct";
  $accesstoken="not included but definitely correct";
  $accesssecret="not included but definitely correct";

  $connection = new TwitterOAuth($apikey, $apisecret, $accesstoken, $accesssecret);

  print_r($connection);

?>
bpr
  • 483
  • 1
  • 11
  • 23
  • That class relies on other classes to function. Instead of including that, try including the autoloader instead: https://github.com/abraham/twitteroauth/blob/master/autoload.php like the documentation says for when you are not using composer https://twitteroauth.com/ – Jeremy Harris Jan 06 '16 at 22:24
  • I'm looking through the autoload.php included in the zip file, will I need to change this variable? $prefix = 'Abraham\\TwitterOAuth\\'; – bpr Jan 06 '16 at 22:28

1 Answers1

0

Adding this as an answer because it is too long for a comment and I suspect is the issue. According to the documentation (https://twitteroauth.com), when not using Composer, you need to include the autoload file. This is what that would look like based on your current code:

<?php

    session_start();

    // Require the autoload file. It registers an autoloader to 
    // know how to access the project's files
    require_once("twitteroauth-master/autoload.php");

    // A use statement so you don't need to use the full namespace
    use Abraham\TwitterOAuth\TwitterOAuth as TwitterOAuth;

    $apikey="not included but definitely correct";
    $apisecret="not included but definitely correct";
    $accesstoken="not included but definitely correct";
    $accesssecret="not included but definitely correct";

    $connection = new TwitterOAuth($apikey, $apisecret, $accesstoken, $accesssecret);

    print_r($connection);

?>

The 500 error is most likely due to it not finding the TwitterOAuth class. Your setup might not be configured correctly to show you errors, which is a whole different matter. You can read about some solutions here: How do I get PHP errors to display?

Another possibility for the 500 error is your require_once fails. If the above code doesn't work, try prepending that file path with ./

Community
  • 1
  • 1
Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133