themattharris / tmhOAuth

An OAuth 1.0A library written in PHP
Apache License 2.0
857 stars 335 forks source link

ERROR: You must enter a query #30

Closed crossplatformdev closed 12 years ago

crossplatformdev commented 12 years ago

Hi Matt.

Im trying to program a simple retwitter bot (basically).

I started making it with twitterOAuth library, and then changed to yours because its capable of more things. The error i got this time is in search( ); function, while trying to perform any search, 'pages' value is not correctly set (I think). This is what var_dump($_POST) and vardump($args) say:

array(6) { ["search"]=> string(25) "#LlamadmeRebeldeSiQuereis" ["selection"]=> string(15) "rt_with_mention" ["string"]=> string(13) "@ManuAlpuente" ["ckey"]=> string(0) "" ["csec"]=> string(0) "" ["ammount"]=> string(1) "5" } array(5) { ["q"]=> string(25) "#LlamadmeRebeldeSiQuereis" ["since_id"]=> string(1) "0" ["pages"]=> string(1) "1" ["rpp"]=> string(1) "5" ["lang"]=> string(2) "es" }

LlamadmeRebeldeSiQuereis: 0: 1: 5: es: array(1) { ["page"]=> int(1) } Received page 0 http://search.twitter.com/search.json?page=1 There was an error. string(55) "{"error":"You must enter a query."}"

So arg['$pages'] should be a string. I found issue #17 and fixed its value to $pages = '0', but no result (same error). Here is the full code (functions are examples of yours).

CODE: http://pastebin.com/H58cRHVa

themattharris commented 12 years ago

The error you are getting back from the Twitter Search API is because your request doesn't specify the 'q' parameter - it's not related to the pages parameter which is mutated into a string when the request URL is built.

Looking at your code http://pastebin.com/H58cRHVa it seems that you are mixing uses. My example script prompts for inputs when executed through the command line. If you want to use it interactively on a webpage you would need to code up an HTML form or hardcode the search values.

In your code the input reading line is line 93. It's this line that won't work in an HTML page, it's designed for command line

$p[$k] = tmhUtilities::read_input("{$v}: ");

The simplest way to call the search API with my code is to do this:

date_default_timezone_set('GMT');

require '../tmhOAuth.php';
require '../tmhUtilities.php';
$tmhOAuth = new tmhOAuth(array());
$tmhOAuth->request(
  'GET',
  'http://search.twitter.com/search.json',
  array(
    'q'        => 'twitter',
    'rpp'      => '100',
  ),
  false
);

if ($tmhOAuth->response['code'] == 200) {
  $data = json_decode($tmhOAuth->response['response'], true);
} else {
  $data = htmlentities($tmhOAuth->response['response']);
  echo 'There was an error.' . PHP_EOL;
  break;
}
var_dump($data);

Last thing: don't share your consumer or oauth secret in public - you can reset them on dev.twitter.com/apps

crossplatformdev commented 12 years ago

Thanks a lot Matt.

I've just changed the keys the first (i noticed what i did really this morning, and i was wondering if create another app or what :). Your code fits pretty well my needs, it runs ok from console and also embedded in a bigger php web (it shows the results with var_dump, I just need to return that array in function search to keep to program running as expected). I also added to your code some items to the array, like intented in the pastebin (make the function capable of advanced searchs based on id, lang, or location).

Effectivelly it works with a form in other side, where you select the terms to search, the action and also could add a string (i.e. retweeting all items in #hashtag to @someone). I made that part the first. It worked with twitterOAuth.php from another developer, but that API is buggy i think.

I surely may change spam and tweet functions, but that would be another issue. Thanks a lot and pardon moi the noobness :)

I mark this as closed.