简体   繁体   中英

Class 'TwitterOAuth' not found cakephp 3

I'm new with cakephp 3. i downloaded the zip file from https://github.com/abraham/twitteroauth . i placed it in ./vendor/twitteroauth-master and in my controller i wrote this code :

<?php
namespace App\Controller;
use App\Controller\AppController;
require_once(ROOT .DS. 'Vendor' . DS . 'twitteroauth-master' . DS . 'autoload.php');
require_once(ROOT .DS. 'Vendor' . DS . 'twitteroauth-master' . DS . 'src' . DS . 'TwitterOAuth.php');

class TweetsController extends AppController
{
   public function index()
   {
$oauth_access_token = '';
        $oauth_access_token_secret = '';
        $consumer_key = '';
        $consumer_secret = '';
 $tw = new \TwitterOAuth($consumer_key,$consumer_secret,$oauth_access_token,$oauth_access_token_secret);
$ret = $tw->get("statuses/home_timeline", array("count" => 10, "exclude_replies" => true));
$this->set('tweets', $tweets);
}}

But i get this error "Class 'TwitterOAuth' not found". Could anyone help ?

so i found a solution to my problem.

i used Composer. you go to your project folder, then you use this command line on your cmd :

composer require abraham/twitteroauth

Then in your controller, you remove the require_once and you put this line of code instead :

use Abraham\TwitterOAuth\TwitterOAuth;

And there you go, your controller can find your class TwitterOAuth now.

But i'm still not getting any tweets. if anyone can help on that, that would be great !! thanks

Did you try using composer?

First, add the library to composer.json with the command

composer require abraham/twitteroauth

This will download the twitteroauth library to your vendor folder and generate autoload files.

Then in your TweetsController.php add use Abraham\\TwitterOAuth\\TwitterOAuth; after use App\\Controller\\AppController;

It should works with these steps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM