Comments (3)
Can you share all your code and also tell me which version of PHP you are running?
from tmhoauth.
Certainly. PHP version is 5.3.8 (using XAMPP 1.7.7 on Windows 7)
I actually think I found what the "problem" is and also answered my own question, but I've included a clarification.
Code (basically the same as the example but modified a bit):
<?php
date_default_timezone_set('GMT');
require '../tmhOAuth.php';
require '../tmhUtilities.php';
$tmhOAuth = new tmhOAuth(array());
$params = array(
'q' => 'Twitter API',
'since_id' => '',
'pages' => '10',
'rpp' => '15',
// 'max_id' => 'Max ID to accept. This isn\'t sent to Search but instead used to filter the received results',
// 'geocode' => 'Geo co-ordinates (e.g. 37.781157,-122.398720,1mi)',
'lang' => 'es'
);
//This sets parameters if using this script through a CLI:
/*
foreach ($params as $k => $v) :
$p[$k] = tmhUtilities::read_input("{$v}: ");
if (empty($p[$k]))
unset($p[$k]);
endforeach;
*/
/* Modification to get it to run without CLI: */
$p = array_filter($params);
//Removes "Notice: Undefined index: pages in..."
if( !isset($p['pages']) ):
$p['pages'] = 1;
endif;
/* End modification. */
$pages = intval($p['pages']);
$pages = $pages > 0 ? $pages : 1;
$results = array();
for ($i=$pages; $i > 0; $i--) {
$args = array_intersect_key(
$p, array(
'q' => '',
'since_id' => '',
'rpp' => '',
'geocode' => '',
'lang' => ''
));
$args['page'] = $i;
$tmhOAuth->request(
'GET',
'http://search.twitter.com/search.json',
$args,
false
);
echo "Received page {$i}\t{$tmhOAuth->url}" . PHP_EOL;
if ($tmhOAuth->response['code'] == 200) {
$data = json_decode($tmhOAuth->response['response'], true);
$results = array_merge($results, $data['results']);
} else {
$data = htmlentities($tmhOAuth->response['response']);
echo 'There was an error.' . PHP_EOL;
var_dump($data);
die();
}
}
foreach ($results as $result) {
$date = strtotime($result['created_at']);
$result['from_user'] = str_pad($result['from_user'], 15, ' ');
$result['text'] = str_replace(PHP_EOL, '', $result['text']);
echo "{$result['id_str']}\t{$date}\t{$result['from_user']}\t\t{$result['text']}" . PHP_EOL;
}
?>
from tmhoauth.
Yeah, that would do it. This example is there just to show how to retrieve results from the Twitter Search API. You could use it as a CLI script, or webpage called by a cron job.
As this script could make multiple requests to the Twitter API (pages) I wouldn't allow it to be reached through a public URL.
There are a lot of optimizations you can make if you know what you want the script to do - for example, don't prompt for the values to search for. Your code does most of this but you can optimize a little more by doing something like this:
<?php
date_default_timezone_set('GMT');
require '../tmhOAuth.php';
require '../tmhUtilities.php';
$tmhOAuth = new tmhOAuth(array());
$pages = 5;
$args = array(
'q' => 'Twitter',
'since_id' => '0',
'rpp' => '15',
'lang' => 'es',
);
$results = array();
for ($i=$pages; $i > 0; $i--) {
$args['page'] = $i;
$tmhOAuth->request(
'GET',
'http://search.twitter.com/search.json',
$args,
false
);
echo "Received page {$i}\t{$tmhOAuth->url}" . PHP_EOL;
if ($tmhOAuth->response['code'] == 200) {
$data = json_decode($tmhOAuth->response['response'], true);
$results = array_merge($results, $data['results']);
} else {
$data = htmlentities($tmhOAuth->response['response']);
echo 'There was an error.' . PHP_EOL;
var_dump($data);
die();
}
}
foreach ($results as $result) {
$date = strtotime($result['created_at']);
$result['from_user'] = str_pad($result['from_user'], 15, ' ');
$result['text'] = str_replace(PHP_EOL, '', $result['text']);
echo "{$result['id_str']}\t{$date}\t{$result['from_user']}\t\t{$result['text']}" . PHP_EOL;
}
?>
As for the strict warning, it's because I don't declare the read_input method as a static function
in the utilities file. It's there as a helper function so if strict compliance is something you need then adding static
should silence the warning.
I'll mark this issue as closed as I think you have found the information you needed - if not feel free to comment and re-open the issue.
from tmhoauth.
Related Issues (20)
- Wrong username password warning if user isn't logged in HOT 1
- update_profile_background_image not updating the bg image (Not a library issue) HOT 1
- 401 Error HOT 1
- error with PHP 5.3 HOT 6
- Using proxies HOT 1
- curl_setopt(): The usage of the @filename API for file uploading is deprecated HOT 2
- 'geocode' parameter doesn't seem to work for search/tweets.json HOT 3
- Auth problems HOT 3
- Not updating image: update_profile_background_image
- php5.6 HOT 11
- Getting an image of original tweet author
- Deprecated "curl_setopt" error on PHP 5.5.24
- Response code 0. HOT 1
- Support for Upload Videos/ Gifs HOT 8
- No composer install instructions HOT 1
- Post with Json
- Parameters for direct message on twitter
- Parameters for direct message on twitter HOT 4
- Rate limiting headers HOT 1
- DELETE requests return HTTP 401: 32 - "Could not authenticate you"
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tmhoauth.