vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB5 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=263)
-   -   vB5 API help - latest / trending posts list only returns if < 5 days (https://vborg.vbsupport.ru/showthread.php?t=328115)

Beergeeksf 06-24-2020 05:32 PM

vB5 API help - latest / trending posts list only returns if < 5 days
 
I created a simple PHP script that loads the vB5 API and generates a simple JSON list of the trending posts. (See sample code below). The problem is that I can't get the scripts to generate anything for more than a couple days. Ideally I'd like to get the trending posts in the last 7 days, last 31 days, etc. But it I go over 3-5 days, nothing is returned. Any suggestions?

Sample code to load the vB5 API, and get the last 20 days of posts:
Code:

$vbpath = $_SERVER['DOCUMENT_ROOT'];
define('CSRF_PROTECTION', false);
require_once($vbpath . '/includes/vb5/autoloader.php');
vB5_Autoloader::register($vbpath);
vB5_Frontend_Application::init('config.php');

$api = Api_InterfaceAbstract::instance();

$search_json = '{"channel":["15"],"starter_only":1,"date":{"from":"'7'"},"sort":{"lastcontent":"desc"},"view":"","exclude_type":["vBForum_PrivateMessage"]}';

$threads = vB_Api::instanceInternal('search')->getInitialResults($search_json, 30, 1);     

$i = 0;
$thread_returns = array();

if (!isset($threads['errors']) AND !empty($threads['results'])) {
  //let's get views too
  $threads['results'] = vB_Api::instanceInternal('node')->mergeNodeviewsForTopics($threads['results']);

  foreach ($threads['results'] as $tid => $thread_details) {
    $i++;
    $thread_returns['results'][$i] = array(
      'title'        => $thread_details['content']['title'],
      'publishdate'  => $thread_details['content']['publishdate'],
      'channeltitle'  => $thread_details['content']['channeltitle'],
      'description'  => $thread_details['content']['description'],
      'userid'        => $thread_details['content']['userid'], 
      'authorname'    => $thread_details['content']['authorname'],
      'uniqueid'      => $thread_details['content']['nodeid']
    );

    //Get author profile page
    if($user_info = vB_User::fetchUserinfo($thread_details['content']['userid'], 0, false, false)) {
      $thread_returns['results'][$i]['user_bbprofileurl'] = vB5_Route::buildUrl('profile|bburl', array('userid' => $thread_details['content']['userid'], 'username' => $thread_details['content']['authorname']));
    }
   
    //vB5_Route::buildUrl -- build out the URL to the post
    $thread_returns['results'][$i]['postlink'] = vB5_Route::buildUrl(
      $thread_details['routeid'] . '|fullurl',
      array(
        'nodeid' => $thread_details['nodeid'],
        'title' => $thread_details['title']
      )
    );

    //vB5_Route::buildUrl -- build out the URL to the channel
    $thread_returns['results'][$i]['channellink'] = vB5_Route::buildUrl( $thread_details['content']['channelroute'] . '|fullurl' );

    //Add in the views and votes
    $thread_returns['results'][$i]['views'] = $thread_details['content']['views'];
    $thread_returns['results'][$i]['votes'] = $thread_details['content']['votes'];

  }
}
$thread_returns['totalRecords'] = count($thread_returns['results']);
$thread_returns['searchJSON'] = $search_json;
$thread_returns['days'] = $num_days;

if ($thread_returns['totalRecords'] > 0) {
  header('Content-Type: application/json');
  echo json_encode($thread_returns);
} else {
  var_dump($thread_returns);
}

If I change the $search_json code above 5 days, nothing is returned, no error is create, just blank output.

Any help is greatly appreciated.

Cheers,
Jeff

delicjous 06-27-2020 04:24 PM

You could use template for this also... just search for initialResult in templates for examples. There is also a ignore cache as search option . You could try this.

Beergeeksf 06-29-2020 05:26 PM

An update -- one of the problems was actually a silent json_encode error in PHP -- once I checked for json errors I saw that error. Wrapping all of the thread details in utf8_encode() fixed that error:

Code:

'title'        => utf8_encode($thread_details['content']['title']),
Still seeing inconsistent results, but at least I get results every time now.

Cheers,
Jeff


All times are GMT. The time now is 12:20 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01192 seconds
  • Memory Usage 1,721KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (2)bbcode_code_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (3)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete