You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This package hides away the complexity of "talking" to the Twitter API, but instead offers a few simple functions to execute some basic queries.
9
12
10
-
## Features ##
13
+
## Features
11
14
12
15
- All queries require an **API key** and **API secret** to generate App Credentials
13
16
- Authorization will be triggered automatically behind the scenes
@@ -17,124 +20,166 @@ This package hides away the complexity of "talking" to the Twitter API, but inst
17
20
- Optional Caching (only [Laravel](http://www.laravel.com/"Laravel") implementation included, see [codezero-be/courier](https://github.com/codezero-be/courier))
18
21
- Optional [Laravel](http://www.laravel.com/"Laravel") ServiceProvider included
19
22
20
-
## Installation ##
23
+
## Installation
21
24
22
25
Install this package through Composer:
23
26
24
-
"require": {
25
-
"codezero/twitter": "1.*"
26
-
}
27
+
```
28
+
"require": {
29
+
"codezero/twitter": "1.*"
30
+
}
31
+
```
32
+
33
+
## Setup
34
+
35
+
### Laravel Setup
36
+
37
+
After installing, update your Laravel `config/app.php` file to include a reference to this package's service provider in the providers array:
27
38
28
-
## Setup ##
39
+
```
40
+
'providers' => [
41
+
'CodeZero\Twitter\TwitterServiceProvider'
42
+
]
43
+
```
29
44
30
-
### Laravel 4 Setup ###
45
+
Next, publish the package config file and [enter your API credentials](#edit-configuration).
31
46
32
-
After installing, update your `app/config/app.php` file to include a reference to this package's service provider in the providers array:
47
+
With Laravel 4.*:
33
48
34
-
'providers' => [
35
-
'CodeZero\Twitter\TwitterServiceProvider'
36
-
]
49
+
```
50
+
php artisan config:publish codezero/twitter
51
+
```
37
52
38
-
Next, create the [config](#edit-configuration"Configuration File") file: `app/config/twitter.php`. Or publish the package config file by running this command in the console:
Specify the location of your config file. An example configuration file is included in the `src/config` folder. You can put this anywhere you want.
45
62
46
-
$config = '/path/to/configFile.php';
63
+
```
64
+
$config = '/path/to/configFile.php';
65
+
```
47
66
48
67
Create Twitter instance:
49
68
50
-
use CodeZero\Twitter\Twitter;
51
-
$twitter = new Twitter($config);
69
+
```
70
+
use CodeZero\Twitter\Twitter;
71
+
$twitter = new Twitter($config);
72
+
```
52
73
53
-
## Edit Configuration ##
74
+
## Edit Configuration
54
75
55
76
Your configuration file should look like the following:
56
77
57
-
<?php
58
-
return [
59
-
'base_url' => 'https://api.twitter.com/',
60
-
'api_version' => '1.1',
61
-
'api_key' => '',
62
-
'api_secret' => ''
63
-
];
78
+
```
79
+
<?php
80
+
return [
81
+
'base_url' => 'https://api.twitter.com/',
82
+
'api_version' => '1.1',
83
+
'api_key' => '',
84
+
'api_secret' => ''
85
+
];
86
+
```
64
87
65
88
Be sure to enter your API key and API secret. Twitter requires this for all requests. Also, do not include the API version in the `base_url` as this would break the authorization request.
66
89
67
-
## Usage ##
90
+
## Usage
68
91
69
-
### Set options ###
92
+
### Set options
70
93
71
94
The number of results to return. Each Twitter request has a maximum limit. If you specify a `$count` greater than this limit, the maximum results will be returned. (Default: `10`)
72
95
73
-
$count = 10;
96
+
```
97
+
$count = 10;
98
+
```
74
99
75
100
The number of minutes the results of the query should be cached. Twitter sets a request limit per hour, so caching is a good idea. Setting this to `0` (zero) will disable caching. (Default: `30`)
76
101
77
-
$cacheMinutes = 30;
102
+
```
103
+
$cacheMinutes = 30;
104
+
```
78
105
79
106
This package includes a `Tweet` object which greatly simplifies the returned results. If you want the full JSON response to be returned, set this to `false`. (Default: `true`)
$error = $e->getMessage(); //=> user not found etc.
123
+
}
124
+
```
95
125
96
-
#### JSON ####
126
+
### Response formats
127
+
128
+
#### JSON
97
129
98
130
If you `$returnEntities` is `false`, you get a `CodeZero\Courier\Response` object, which contains the actual JSON response.
99
131
100
-
$tweets = $twitter->getTweetsFromUser($username);
132
+
```
133
+
$tweets = $twitter->getTweetsFromUser($username);
101
134
102
-
echo $tweets; //=> Print the JSON
103
-
$json = $tweets->getBody(); //=> Returns the JSON
104
-
$array = $tweets->toArray(); //=> Convert JSON to an array
135
+
echo $tweets; //=> Print the JSON
136
+
$json = $tweets->getBody(); //=> Returns the JSON
137
+
$array = $tweets->toArray(); //=> Convert JSON to an array
138
+
```
105
139
106
140
For more information on this `Response` object, refer to [codezero-be/courier](https://github.com/codezero-be/courier).
107
141
108
-
#### Tweets ####
142
+
#### Tweets
109
143
110
144
If you `$returnEntities` is `true`, you get an array of `CodeZero\Twitter\Entities\Tweet` objects. This is a very simplified `Tweet` object, which only contains the most useful info about the tweet and its user.
111
145
112
-
$tweets = $twitter->getTweetsFromUser($username);
113
-
114
-
foreach ($tweets as $tweet)
115
-
{
116
-
$user = $tweet->user();
117
-
$tweetOwner = $user->getName();
118
-
$tweetUsername = $user->getUsername();
119
-
$tweetText = $tweet->getText();
120
-
$tweetDate = $tweet->getCreatedAt();
121
-
}
146
+
```
147
+
$tweets = $twitter->getTweetsFromUser($username);
148
+
```
149
+
150
+
151
+
152
+
```
153
+
foreach ($tweets as $tweet)
154
+
{
155
+
$user = $tweet->user();
156
+
$tweetOwner = $user->getName();
157
+
$tweetUsername = $user->getUsername();
158
+
$tweetText = $tweet->getText();
159
+
$tweetDate = $tweet->getCreatedAt();
160
+
}
161
+
```
122
162
123
163
For an overview of all available `Tweet` and `User` information, take a look at the source.
0 commit comments