-2

I am using Cakephp 3, and i want to retreive tweets using Twitter Api seach using https://packagist.org/packages/j7mbo/twitter-api-php. This is my code :

class TweetsController extends AppController
{
 public function index()
{
    $settings = array(
'oauth_access_token' => 'myaccestoken',
        'oauth_access_token_secret' => 'myaccestokensecret',
        'consumer_key' => 'myconsumerkey',
        'consumer_secret' => 'myconsumerkeysecret'
);
    $url = 'https://api.twitter.com/1.1/followers/ids.json';
$getfield = '?screen_name=J7mbo';
$requestMethod = 'GET';

$twitter = new TwitterAPIExchange($settings);
echo $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
    $this->set('tweets', $tweets);
}
}

But it's not working, can anyone help ? Thanks

Jack
  • 3
  • 1

2 Answers2

0

If you are using the twitterAPIExchange.php you can use it like this:

<?php
ini_set('display_errors', 1);
require_once('TwitterAPIExchange.php');
$settings = array(
'oauth_access_token' => "",
'oauth_access_token_secret' => "",
'consumer_key' => "",
'consumer_secret' => ""
);
if(isset($_GET["user_id"])){
$userId=$_GET["user_id"];
$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=' . $userId . "&count=20";
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
         ->buildOauth($url, $requestMethod)
         ->performRequest();
}
echo $response;
?>

You can change the GET param name to accomodate your needs.

In this case im using user_id so the URL should look like this:

localhost/?user_id=jack

In your code you are just using the wrong $url and make sure to handle the response the way you want.

Danny Sandi
  • 657
  • 7
  • 27
  • I get this error when i try to retreive the tweets : SSL certificate problem: unable to get local issuer certificate. – Jack Aug 21 '15 at 19:35
  • @Jack Thats an issue with your server, read more here: http://stackoverflow.com/questions/28858351/php-ssl-certificate-error-unable-to-get-local-issuer-certificate. – Danny Sandi Aug 21 '15 at 19:39
0

I use the "J7mbo/twitter-api-php" lib as vendor in CakePHP3 and did it like this:

1) add this to your composer.json:

"require": { ... "j7mbo/twitter-api-php": "dev-master" },

Then run composer update (php composer.phar update)

2) Now you can use the TwitterAPIExchange class in your controller

$settings = array(
  'oauth_access_token' => "YOUR_OAUTH_ACCESS_TOKEN",
  'oauth_access_token_secret' => "YOUR_OAUTH_ACCESS_TOKEN_SECRET",
  'consumer_key' => "YOUR_CONSUMER_KEY",
  'consumer_secret' => "YOUR_CONSUMER_SECRET"
);
$twitter = new TwitterAPIExchange($settings);
// Example: get J7mbo last tweets
$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=J7mbo';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest();
$this->set('response', json_decode($response));
hasentopf
  • 755
  • 1
  • 12
  • 27