|
1 | 1 | stream-php |
2 | 2 | ========== |
3 | 3 |
|
4 | | -[](https://travis-ci.org/GetStream/stream-php) [](https://coveralls.io/github/GetStream/stream-php?branch=master) [](http://badge.fury.io/ph/get-stream%2Fstream) |
| 4 | +[](https://travis-ci.org/GetStream/stream-php) |
| 5 | +[](https://coveralls.io/github/GetStream/stream-php?branch=master) |
| 6 | +[](https://packagist.org/packages/get-stream/stream) |
5 | 7 |
|
6 | 8 | [stream-php](https://github.com/GetStream/stream-php) is the official PHP client for [Stream](https://getstream.io/), a web service for building scalable newsfeeds and activity streams. |
7 | 9 |
|
8 | | -Note that there is also a [higher level Laravel - Stream integration](https://github.com/getstream/stream-laravel) which hooks into the Laravel ORM. |
| 10 | +Note that there is also a higher level [Laravel](https://github.com/getstream/stream-laravel) integration which hooks into the Eloquent ORM. |
9 | 11 |
|
10 | 12 | You can sign up for a Stream account at https://getstream.io/get_started. |
11 | 13 |
|
12 | 14 | ### Installation |
13 | 15 |
|
14 | | -#### PHP version requirements |
| 16 | +#### Composer |
15 | 17 |
|
16 | | -This API Client project requires PHP 5.5 at a minimum. |
| 18 | +``` |
| 19 | +composer require get-stream/stream |
| 20 | +``` |
17 | 21 |
|
18 | | -See the [Travis configuration](.travis.yml) for details of how it is built, tested and packaged. |
| 22 | +Composer will install our latest version automatically. |
19 | 23 |
|
20 | | -#### Install with Composer |
| 24 | +#### PHP compatibility |
21 | 25 |
|
22 | | -If you're using [Composer](https://getcomposer.org/) to manage |
23 | | -dependencies, you can add Stream with it. |
| 26 | +Current releases require PHP `5.5` or higher, and depends on Guzzle version 6. |
24 | 27 |
|
25 | | -```javascript |
26 | | -{ |
27 | | - "require": { |
28 | | - "get-stream/stream": "$VERSION" |
29 | | - } |
30 | | -} |
31 | | -``` |
| 28 | +If you need to use the client with PHP 5.4 or earlier versions of Guzzle, you can grab an earlier version of this package: |
32 | 29 |
|
33 | | -(replace `$VERSION` with one of the available versions on |
34 | | -[Packagist](https://packagist.org/packages/get-stream/stream)) |
| 30 | +``` |
| 31 | +composer require get-stream/stream:"~2.1.0" |
| 32 | +``` |
35 | 33 |
|
36 | | -Composer will take care of the autoloading for you, so if you require |
37 | | -the `vendor/autoload.php`, you're good to go. |
| 34 | +See the [Travis configuration](.travis.yml) for details of how it is built and tested against different PHP versions. |
38 | 35 |
|
39 | | -### Full documentatation |
| 36 | +### Documentatation |
40 | 37 |
|
41 | | -Documentation for this PHP client are available at the [Stream website](https://getstream.io/docs/?language=php). |
| 38 | +Our full documentation for this package is available at [https://getstream.io/docs/php/](https://getstream.io/docs/php/). |
42 | 39 |
|
43 | | -### Usage |
| 40 | +#### Quick start |
44 | 41 |
|
45 | | -```php |
46 | | -<?php |
| 42 | +First, [signup here](https://getstream.io/dashboard/) for a free account and grab your API key and secret. |
47 | 43 |
|
48 | | -require_once __DIR__ . '/vendor/autoload.php'; |
| 44 | +Initiating a Client and a Feed object: |
49 | 45 |
|
50 | | -// Instantiate a new client, find your API keys here https://getstream.io/dashboard/ |
51 | | -$client = new GetStream\Stream\Client('YOUR_API_KEY', 'API_KEY_SECRET'); |
52 | | - |
53 | | -// Set API endpoint location |
54 | | -$client->setLocation('us-east'); |
| 46 | +```php |
| 47 | +// Instantiate a new client, find your API keys in the dashboard. |
| 48 | +$client = new GetStream\Stream\Client('YOUR_API_KEY', 'YOUR_API_SECRET'); |
55 | 49 |
|
56 | 50 | // Instantiate a feed object |
57 | | -$user_feed_1 = $client->feed('user', '1'); |
| 51 | +$userFeed = $client->feed('user', '1'); |
58 | 52 |
|
59 | | -// Get 20 activities starting from activity with id $last_id (fast id offset pagination) |
60 | | -$results = $user_feed_1->getActivities(0, 20, $last_id); |
| 53 | +``` |
61 | 54 |
|
62 | | -// Get 10 activities starting from the 5th (slow offset pagination) |
63 | | -$results = $user_feed_1->getActivities(5, 10); |
| 55 | +Activities in a feed: |
64 | 56 |
|
| 57 | +```php |
65 | 58 | // Create a new activity |
66 | 59 | $data = [ |
67 | | - "actor"=>"1", |
68 | | - "verb"=>"like", |
69 | | - "object"=>"3", |
70 | | - "foreign_id"=>"post:42" |
71 | | -]; |
72 | | -$user_feed_1->addActivity($data); |
73 | | -// Create a bit more complex activity |
74 | | -$now = new \DateTime("now", new \DateTimeZone('Pacific/Nauru')); |
75 | | -$data = ['actor' => 1, 'verb' => 'run', 'object' => 1, 'foreign_id' => 'run:1', |
76 | | - 'course' => ['name'=> 'Golden Gate park', 'distance'=> 10], |
77 | | - 'participants' => ['Thierry', 'Tommaso'], |
78 | | - 'started_at' => $now |
| 60 | + 'actor' => '1', |
| 61 | + 'verb' => 'like', |
| 62 | + 'object' => '3', |
| 63 | + 'foreign_id' => 'like:42', |
79 | 64 | ]; |
80 | | -$user_feed_1->addActivity($data); |
81 | 65 |
|
82 | | -// Remove an activity by its id |
83 | | -$user_feed_1->removeActivity("e561de8f-00f1-11e4-b400-0cc47a024be0"); |
| 66 | +$response = $userFeed->addActivity($data); |
84 | 67 |
|
85 | | -// Remove activities by their foreign_id |
86 | | -$user_feed_1->removeActivity('post:42', true); |
| 68 | +// The response will include Stream's internal ID: |
| 69 | +// {"id": "e561...", "actor": "1", ...} |
87 | 70 |
|
88 | | -// Let user 1 start following user 42's flat feed |
89 | | -$user_feed_1->followFeed('flat', '42'); |
| 71 | +// Get the latest activities for this user's personal feed, based on who they are following. |
| 72 | +$response = $userFeed->getActivities(); |
90 | 73 |
|
91 | | -// Let user 1 start following user 42's flat feed but only copy 10 activities to target feed |
92 | | -$user_feed_1->followFeed('flat', '42', 10); |
| 74 | +// The response will be the json decoded API response. |
| 75 | +// {"duration": 45ms, "next": "/api/v1.0/feed/...", "results": [...]} |
93 | 76 |
|
94 | | -// Let user 1 stop following user 42's flat feed |
95 | | -$user_feed_1->unfollowFeed('flat', '42'); |
| 77 | +// Remove an activity by its ID |
| 78 | +$userFeed->removeActivity('e561de8f-00f1-11e4-b400-0cc47a024be0'); |
96 | 79 |
|
97 | | -// Let user 1 stop following user 42's flat feed but keep the history in its feed |
98 | | -$user_feed_1->unfollowFeed('flat', '42', true); |
| 80 | +// To remove activities by their foreign_id, set the "foreign id" flag to true. |
| 81 | +$userFeed->removeActivity('like:42', true); |
| 82 | +``` |
99 | 83 |
|
100 | | -// Batch adding activities |
101 | | -$activities = array( |
102 | | - array('actor' => '1', 'verb' => 'tweet', 'object' => '1'), |
103 | | - array('actor' => '2', 'verb' => 'like', 'object' => '3') |
104 | | -); |
105 | | -$user_feed_1->addActivities($activities); |
| 84 | +Following/follower relations of a feed: |
106 | 85 |
|
107 | | -// Add an activity and push it to other feeds too using the `to` field |
108 | | -$data = [ |
109 | | - "actor"=>"1", |
110 | | - "verb"=>"like", |
111 | | - "object"=>"3", |
112 | | - "to"=>["user:44", "user:45"] |
113 | | -]; |
114 | | -$user_feed_1->addActivity($data); |
| 86 | +```php |
| 87 | +// When user 1 starts following user 37's activities |
| 88 | +$userFeed->followFeed('user', '37'); |
115 | 89 |
|
116 | | -// Delete a feed (and its content) |
117 | | -$user_feed_1->delete(); |
| 90 | +// When user 1 stops following user 37's activities |
| 91 | +$userFeed->unfollowFeed('user', '37'); |
118 | 92 |
|
119 | | -// Generating tokens for client side usage |
120 | | -$token = $user_feed_1->getToken(); |
| 93 | +// Retrieve followers of a feed |
| 94 | +$userFeed->followers(); |
121 | 95 |
|
122 | | -// Javascript client side feed initialization |
123 | | -// user1 = client.feed('user', '1', "$token"); |
| 96 | +// Retrieve feeds followed by $userFeed |
| 97 | +$userFeed->following(); |
| 98 | +``` |
124 | 99 |
|
125 | | -// Generating read-only tokens for client side usage |
126 | | -$readonlyToken = $user_feed_1->getReadonlyToken(); |
| 100 | +Advanced activity operations: |
127 | 101 |
|
128 | | -// Javascript client side feed initialization (readonly) |
129 | | -// user1 = client.feed('user', '1', "$readonlyToken"); |
| 102 | +```php |
| 103 | +// Create a bit more complex activity with custom fields |
| 104 | +$data = [ |
| 105 | + 'actor' => 1, |
| 106 | + 'verb' => 'run', |
| 107 | + 'object' => 1, |
| 108 | + 'foreign_id' => 'run:42', |
| 109 | + |
| 110 | + // Custom fields: |
| 111 | + 'course' => [ |
| 112 | + 'name'=> 'Golden Gate park', |
| 113 | + 'distance'=> 10, |
| 114 | + ], |
| 115 | + 'participants' => ['Thierry', 'Tommaso'], |
| 116 | + 'started_at' => new DateTime('now', new DateTimeZone('Pacific/Nauru'), |
| 117 | +]; |
130 | 118 |
|
131 | | -// Retrieve first 10 followers of a feed |
132 | | -$user_feed_1->followers(0, 10); |
| 119 | +// Add an activity and push it to other feeds too using the `to` field |
| 120 | +$data = [ |
| 121 | + 'actor' => '1', |
| 122 | + 'verb' => 'like', |
| 123 | + 'object' => '3', |
| 124 | + 'to' => [ |
| 125 | + 'user:44', |
| 126 | + 'user:45', |
| 127 | + ], |
| 128 | +]; |
133 | 129 |
|
134 | | -// Retrieve 2 to 10 followers |
135 | | -$user_feed_1->followers(2, 10); |
| 130 | +$userFeed->addActivity($data); |
| 131 | + |
| 132 | +// Batch adding activities |
| 133 | +$activities = [ |
| 134 | + ['actor' => '1', 'verb' => 'tweet', 'object' => '1'], |
| 135 | + ['actor' => '2', 'verb' => 'like', 'object' => '3'], |
| 136 | +]; |
136 | 137 |
|
137 | | -// Retrieve 10 feeds followed by $user_feed_1 |
138 | | -$user_feed_1->following(0, 10); |
| 138 | +$userFeed->addActivities($activities); |
139 | 139 |
|
140 | | -// Retrieve 10 feeds followed by $user_feed_1 starting from the 10th (2nd page) |
141 | | -$user_feed_1->following(10, 20); |
| 140 | +// Delete an entire feed and its content |
| 141 | +$userFeed->delete(); |
| 142 | +``` |
142 | 143 |
|
143 | | -// Check if $user_feed_1 follows specific feeds |
144 | | -$user_feed_1->following(0, 2, ['user:42', 'user:43']); |
| 144 | +Advanced batching: |
145 | 145 |
|
| 146 | +```php |
146 | 147 | // Batch operations (batch activity add, batch follow) |
147 | 148 | $batcher = $client->batcher(); |
148 | 149 |
|
149 | 150 | // Add one activity to many feeds |
150 | | -$activity = array('actor' => '1', 'verb' => 'tweet', 'object' => '1'); |
151 | | -$feeds = ['flat:user1', 'flat:user2']; |
| 151 | +$activity = ['actor' => '1', 'verb' => 'tweet', 'object' => '1']; |
| 152 | +$feeds = ['user:1', 'user:2']; |
| 153 | + |
152 | 154 | $batcher->addToMany($activity, $feeds); |
153 | 155 |
|
154 | | -// Create many follow |
| 156 | +// Create many follow relations |
155 | 157 | $follows = [ |
156 | | - ['source' => 'flat:b1', 'target' => 'user:b1'], |
157 | | - ['source' => 'flat:b1', 'target' => 'user:b3'] |
| 158 | + ['source' => 'user:b1', 'target' => 'user:b2'], |
| 159 | + ['source' => 'user:b1', 'target' => 'user:b3'], |
158 | 160 | ]; |
| 161 | + |
159 | 162 | $batcher->followMany($follows); |
| 163 | +``` |
| 164 | + |
| 165 | +Generating tokens for client-side usage: |
160 | 166 |
|
161 | | -// Create many follows without copying activities |
162 | | -$batcher->followMany($follows, 0); |
| 167 | +```php |
| 168 | +// Generating a client side token |
| 169 | +$token = $userFeed->getToken(); |
| 170 | + |
| 171 | +// Generating a read-only client side token |
| 172 | +$readonlyToken = $userFeed->getReadonlyToken(); |
163 | 173 | ``` |
164 | 174 |
|
165 | | -### Contributing |
| 175 | +Again, our full documentation with all options and methods, is available at [https://getstream.io/docs/php/](https://getstream.io/docs/php/). |
166 | 176 |
|
167 | | -First, make sure you can run the test suite. Install development |
168 | | -dependencies : |
| 177 | +### Framework integration |
169 | 178 |
|
170 | | - $ composer install |
| 179 | +#### Laravel |
171 | 180 |
|
172 | | -You may now use phpunit : |
| 181 | +There's a higher level integration with [Laravel](https://laravel.com) called [`get-stream/stream-laravel`](https://github.com/getstream/stream-laravel). |
| 182 | +The `stream-laravel` integration helps you to hook into the Laravel's Eloquent ORM to sync data to Stream. |
173 | 183 |
|
174 | | - $ vendor/bin/phpunit |
| 184 | +### Contributing |
| 185 | + |
| 186 | +We love contributions. We love contributions with tests even more! To run the test-suite to ensure everything still works, run phpunit: |
| 187 | + |
| 188 | +``` |
| 189 | +vendor/bin/phpunit --testsuite "Unit Test Suite" |
| 190 | +``` |
175 | 191 |
|
176 | 192 | ### Copyright and License Information |
177 | 193 |
|
|
0 commit comments