Search code examples
phptwitter

I used the TwitterOAuth library and got this error


Fatal error: Uncaught Error: Class 'Composer\CaBundle\CaBundle' not found in 
C:\xampp\htdocs\CompleteWDC\cha10_APIs\twitter-api\twitteroauth\src\TwitterOAuth.php:572 Stack trace: 
#0 C:\xampp\htdocs\CompleteWDC\cha10_APIs\twitter-api\twitteroauth\src\TwitterOAuth.php(620): Abraham\TwitterOAuth\TwitterOAuth->curlOptions() 
#1 C:\xampp\htdocs\CompleteWDC\cha10_APIs\twitter-api\twitteroauth\src\TwitterOAuth.php(557): Abraham\TwitterOAuth\TwitterOAuth->request('https://api.twi...', 'GET', 'Authorization: ...', Array, false) 
#2 C:\xampp\htdocs\CompleteWDC\cha10_APIs\twitter-api\twitteroauth\src\TwitterOAuth.php(490): Abraham\TwitterOAuth\TwitterOAuth->oAuthRequest('https://api.twi...', 'GET', Array, false) 
#3 C:\xampp\htdocs\CompleteWDC\cha10_APIs\twitter-api\twitteroauth\src\TwitterOAuth.php(467): Abraham\TwitterOAuth\TwitterOAuth->makeRequests('https://api.twi...', 'GET', Array, false) 
#4 C:\xampp\htdocs\CompleteWDC\cha10_APIs\twitter-api\twitteroauth\src\TwitterOAuth.php(232): Abraham\TwitterOAuth\TwitterOAuth->http('GET', 'h in C:\xampp\htdocs\CompleteWDC\cha10_APIs\twitter-api\twitteroauth\src\TwitterOAuth.php on line 572

I'm using it for the first time, so checked the TwitterOAuth.php file and line 572 then vscode says the CaBundle is undefined Type Undefined type 'Composer\CaBundle\CaBundle'. , I tried downloading the file over and over again but the same error.


Solution

  • I had the exact same problem following the instructions on here.

    It turns out I wasn't including the right autoload. For those coming across the same problem:

    1. Install composer locally on your directory as per these instructions.
    2. Run php composer require abraham/twitteroauth.
    3. Run php composer install.
    4. Make sure to include the right path to the composer autoloader file, not the package autoloader, in your PHP code require "vendor/autoload.php";.
    5. In each file that you will be using the package, don't forget to include the line use Abraham\TwitterOAuth\TwitterOAuth as TwitterOAuth;, so that you can use $foo = new TwitterOAuth();

    I hope this helps those who might be new to composer like me.