Skip to content

Add PHP samples #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,6 @@ build-iPhoneSimulator/

# Used by RuboCop. Remote config files pulled in from inherit_from directive.
# .rubocop-https?--*

# Used by JetBrains IDEs
.idea/
174 changes: 174 additions & 0 deletions Filtered-Stream/filtered_stream.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php

/**
* To use these samples you will need to set your bearer token as an environment variable by running the following in your terminal:
* export BEARER_TOKEN='<your_bearer_token>'
*/

/**
* API endpoints
*/
const FILTERED_STREAM_API_URL = 'https://api.twitter.com/2/tweets/search/stream';
const FILTERED_STREAM_RULES_API_URL = 'https://api.twitter.com/2/tweets/search/stream/rules';

/**
* Sample rules for testing
*/
const SAMPLE_RULES = [
[
'value' => "cat has:media",
'tag' => "cats with media"
],
[
'value' => "cat has:media -grumpy",
'tag' => "happy cats with media"
],
[
'value' => "meme",
'tag' => "funny things"
],
[
'value' => "meme has:images",
],
];

/**
* Retrieve existing rules
*
* @throws Exception
*/
function get_rules(array $headers): array
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, FILTERED_STREAM_RULES_API_URL);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
$status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

$decoded_response = json_decode($response, true);

if ($status_code != 200) {
$errors = print_r($decoded_response['errors'] ?? $decoded_response, true);

throw new Exception("Unable to retrieve rules: ".$errors, $status_code);
}

return $decoded_response['data'] ?? [];
}

/**
* Delete a set of rules
*
* @throws Exception
*/
function delete_rules(array $rules, array $headers): array
{
$headers[] = 'Content-Type: application/json';

// extracting the ids from the rules array
$ids = array_map(function($rule) {
return $rule['id'];
}, $rules);

$params = [
'delete' => [
'ids' => $ids
]
];

$url = FILTERED_STREAM_RULES_API_URL.'?'.http_build_query($params);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
$status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

$decoded_response = json_decode($response, true);

if ($status_code != 200) {
$errors = print_r($decoded_response['errors'] ?? $decoded_response, true);

throw new Exception("Unable to delete rules: ".$errors, $status_code);
}

return $decoded_response;
}

/**
* Set new rules
*
* @throws Exception
*/
function set_rules(array $rules, array $headers): array
{
$headers[] = 'Content-Type: application/json';

$params = [
'add' => $rules
];

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, FILTERED_STREAM_RULES_API_URL);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params));
$response = curl_exec($curl);
$status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

$decoded_response = json_decode($response, true);

if (!in_array($status_code, [200, 201])) {
$errors = print_r($decoded_response['errors'] ?? $decoded_response, true);

throw new Exception("Unable to set rules: ".$errors, $status_code);
}

return $decoded_response['data'] ?? [];
}

/**
* @throws Exception
*/
function stream(array $headers): void
{
// handle what to do with the tweets in this function, in this example we'll be printing the response
$callback = function($ch, $str) {
print $str;

// remove this line if you only want to use one tweet
return strlen($str);
};

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, FILTERED_STREAM_API_URL);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_WRITEFUNCTION, $callback);
curl_exec($curl);
curl_close($curl);
}

try {
// default headers to use with every request
$headers = [
'Authorization: Bearer '.getenv('BEARER_TOKEN')
];

$rules = get_rules($headers);

$deleted_rules = delete_rules($rules, $headers);

$rules = set_rules(SAMPLE_RULES, $headers);

// start the stream
stream($headers);
} catch (Exception $e) {
print $e->getMessage();
}
53 changes: 53 additions & 0 deletions Follows-Lookup/followers_lookup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/**
* To use these samples you will need to set your bearer token as an environment variable by running the following in your terminal:
* export BEARER_TOKEN='<your_bearer_token>'
*/

const FOLLOWERS_API_URL = 'https://api.twitter.com/2/users/%u/followers';

/**
* @throws Exception
*/
function get_followers(int $user_id, array $headers, array $params = []): array
{
$url = sprintf(FOLLOWERS_API_URL, $user_id).'?'.http_build_query($params);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
$status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

$decoded_response = json_decode($response, true);

if ($status_code != 200) {
$errors = print_r($decoded_response['errors'] ?? $decoded_response, true);

throw new Exception("Unable to get followers: ".$errors, $status_code);
}

return $decoded_response['data'] ?? [];
}

try {
// replace with the user id to pull followers from
$user_id = 2244994945;

$headers = [
'Authorization: Bearer '.getenv('BEARER_TOKEN')
];

// update the array below with your parameters:
// https://developer.twitter.com/en/docs/twitter-api/users/follows/api-reference/get-users-id-followers
$params = [
'user.fields' => 'created_at'
];

$followers = get_followers($user_id, $headers, $params);
} catch (Exception $e) {
print $e->getMessage();
}
53 changes: 53 additions & 0 deletions Follows-Lookup/following_lookup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/**
* To use these samples you will need to set your bearer token as an environment variable by running the following in your terminal:
* export BEARER_TOKEN='<your_bearer_token>'
*/

const FOLLOWING_API_URL = 'https://api.twitter.com/2/users/%u/following';

/**
* @throws Exception
*/
function get_following_users(int $user_id, array $headers, array $params = []): array
{
$url = sprintf(FOLLOWING_API_URL, $user_id).'?'.http_build_query($params);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
$status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

$decoded_response = json_decode($response, true);

if ($status_code != 200) {
$errors = print_r($decoded_response['errors'] ?? $decoded_response, true);

throw new Exception("Unable to get followers: ".$errors, $status_code);
}

return $decoded_response['data'] ?? [];
}

try {
// replace with the user id to pull following users from
$user_id = 2244994945;

$headers = [
'Authorization: Bearer '.getenv('BEARER_TOKEN')
];

// update the array below with your parameters:
// https://developer.twitter.com/en/docs/twitter-api/users/follows/api-reference/get-users-id-following
$params = [
'user.fields' => 'created_at'
];

$following_users = get_following_users($user_id, $headers, $params);
} catch (Exception $e) {
print $e->getMessage();
}
51 changes: 51 additions & 0 deletions Full-Archive-Search/full_archive_search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* To use these samples you will need to set your bearer token as an environment variable by running the following in your terminal:
* export BEARER_TOKEN='<your_bearer_token>'
*/

const SEARCH_ALL_TWEETS_API_URL = 'https://api.twitter.com/2/tweets/search/all';

/**
* @throws Exception
*/
function get_all_tweets(array $headers, array $params = []): array
{
$url = SEARCH_ALL_TWEETS_API_URL.'?'.http_build_query($params);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
$status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

$decoded_response = json_decode($response, true);

if ($status_code != 200) {
$errors = print_r($decoded_response['errors'] ?? $decoded_response, true);

throw new Exception("Unable to get tweets: ".$errors, $status_code);
}

return $decoded_response['data'] ?? [];
}

try {
$headers = [
'Authorization: Bearer '.getenv('BEARER_TOKEN')
];

// update the array below with your parameters:
// https://developer.twitter.com/en/docs/twitter-api/tweets/search/api-reference/get-tweets-search-all
$params = [
'query' => '(from:twitterdev -is:retweet) OR #twitterdev',
'tweet.fields' => 'author_id'
];

$tweets = get_all_tweets($headers, $params);
} catch (Exception $e) {
print $e->getMessage();
}
52 changes: 52 additions & 0 deletions Full-Archive-Tweet-Counts/full_archive_tweet_counts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/**
* To use these samples you will need to set your bearer token as an environment variable by running the following in your terminal:
* export BEARER_TOKEN='<your_bearer_token>'
*/

const ALL_TWEET_COUNTS_API_URL = 'https://api.twitter.com/2/tweets/counts/all';

/**
* @throws Exception
*/
function get_all_tweet_counts(array $headers, array $params = []): array
{
$url = ALL_TWEET_COUNTS_API_URL.'?'.http_build_query($params);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
$status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

$decoded_response = json_decode($response, true);

if ($status_code != 200) {
$errors = print_r($decoded_response['errors'] ?? $decoded_response, true);

throw new Exception("Unable to get tweet counts: ".$errors, $status_code);
}

return $decoded_response['data'] ?? [];
}

try {
$headers = [
'Authorization: Bearer '.getenv('BEARER_TOKEN')
];

// update the array below with your parameters:
// https://developer.twitter.com/en/docs/twitter-api/tweets/counts/api-reference/get-tweets-counts-all
$params = [
'query' => 'from:twitterdev',
'granularity' => 'day',
'start_time' => '2021-01-01T00:00:00Z'
];

$tweet_counts = get_all_tweet_counts($headers, $params);
} catch (Exception $e) {
print $e->getMessage();
}
Loading