Coder Social home page Coder Social logo

Comments (3)

themattharris avatar themattharris commented on July 29, 2024

Can you share all your code and also tell me which version of PHP you are running?

from tmhoauth.

kenshin23 avatar kenshin23 commented on July 29, 2024

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.

themattharris avatar themattharris commented on July 29, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.