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