PDA

View Full Version : vB5 API help - latest / trending posts list only returns if < 5 days


Beergeeksf
06-24-2020, 05:32 PM
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:

$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:


'title' => utf8_encode($thread_details['content']['title']),


Still seeing inconsistent results, but at least I get results every time now.

Cheers,
Jeff