Go Back   vb.org Archive > vBulletin 5 Connect Discussion > vB5 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 10-15-2017, 11:44 PM
jonijoba jonijoba is offline
 
Join Date: Oct 2017
Posts: 2
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default VB5 API Examples

Hi there!

I've been playing with the Vb 5.2.5 api and I must say... woo what a mess !

The code seems fine but it's really hard for beginners to find complete examples / tutorials that work at first attempt. And that's a shame because that should be working in 5 minutes otherwise.

1. Is there a good tutorial somewhere on the Web with simple vb api examples (php scripts for internal and external use) ?
2. Could someone share its php scripts or experience ?

Thanks in advance,
Joni

Here is the code i use, made from examples over the web (that works at least for my basic setup VB5.2.5).

At the moment, it's not perfect but can :
- create a new thread
- create a new user

Hope it could the beginning of something bigger, like vbulletin.bridge class functions (https://github.com/EQdkpPlus/core/bl...idge.class.php) but with vb5 api instead of datamanager.
As a beginner, I've perharps misunterstood or missed something. Please let me know!





<?php

// Connection
$admin = 'your_admin_name';
$api = init_api ($admin);

// New User Registration
$username = 'new_user';
$password = 'pwd';
$email = 'email@htomail.com';
//add_user ($username, $password, $email, $api);

// Add new Thread
$user = 'new_user or admin';
$title = "Title";
$mainText = "lorem ipsu blabla";
add_thread ($user, $title, $mainText, $api);


function init_api ($admin) {

define("CSRF_PROTECTION", false);
require_once(__DIR__ . '/includes/vb5/autoloader.php');
vB5_Autoloader::register(__DIR__);
vB5_Frontend_Application::init('config.php');
vB::getDbAssertor()->delete("session", array("sessionhash" => vB::getCurrentSession()->get("dbsessionhash")));

$username = vB_String::htmlSpecialCharsUni($admin);
$userinfo = vB::getDbAssertor()->getRow("user", array("username" => $username));
$auth = array_intersect_key($userinfo, array_flip(["userid","secret","lastvisit", "lastactivity"]));
$loginInfo = vB_User:rocessNewLogin($auth);
vB5_Auth::setLoginCookies($loginInfo,"",false);
$api = Api_InterfaceAbstract::instance();

return $api;
}

function add_thread ($user, $title, $mainText, $api) {

$options = [];
$wakht = time();

$input = [
"publisheddate" =>$wakht,
"userid" => 1,
"authorname" => $user,
"description" => '',
"pagetext" => "",
"rawtext" => $mainText,
"title" => $title,
"htmltitle" => $title,
"parentid" => 3,
"created" => $wakht,
"lastcontent" => $wakht,
"lastcontentauthor" => $user,
"lastauthorid" => 1,
"hvinput"=>""
];

$nodee = $api->callApi("content_text", "add", [$input, $options]);

// for some reasons that i was not able to figure out the post is added as guest so i had to updated the node.
$changes = array(
'authorname'=> $username,
'lastcontentauthor'=> $username,
);

vB::getDbAssertor()->update('vBForum:node', $changes, array('nodeid' => $nodee));
vB_Cache::allCacheEvent("nodeChg_" .$nodee);
}



function add_user ($username, $password, $email, $api) {

$data = array(
'userid' => 0,
'password' => $password,
'user' => array('username' => $username, 'email' => $email),
array(),
array(),
'userfield' => false,
array(),
'',
array('registration' => true)
);

$response = $api->callApi('user', 'save', $data, false, true);

}


?>

--------------- Added [DATE]1508118515[/DATE] at [TIME]1508118515[/TIME] ---------------

This script only works with internal use,
I mean inside the forum root directory.

--------------- Added [DATE]1508170351[/DATE] at [TIME]1508170351[/TIME] ---------------

OK i found another code, but this time for external access.

The login works on Vb4 but unfortunately fails with Vb525 (login + adduser functions failed)
This time I've taken care to note the source : http://forum.vbulletin-germany.com/a...p/t-58641.html

It returns null with the following error message with vb5 (both while testinf login and new user function):

Warning: fopen(http://localhost:8888/vb525/api.php?...cookieuser=1): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /Applications/MAMP/htdocs/vb525/testapi6.php on line 170

Anyone with the same problem? or with any solution?

Thanks in advance,
Joni




<?php
//Parameters
$clientname = "mysuperclient";
$clientversion ="0.1";
$platformname = "example";
$platformversion ="2.5";
$uniqueid = "1234";
$apiurl="http://localhost:8888/vb525/api.php"; // Edit here
$apikey = 'xxxxxxxx'; // Edit here

error_reporting(E_ALL);

///////////////////////////////////////// ACTION

if(isset($_POST['username']) && isset($_POST['password'])) {
$userName = $_POST['username'];
$password = $_POST['password'];

if($userName != '' && $password != '') {
$jsarray = initApiRequest($clientname, $clientversion, $platformname, $platformversion, $uniqueid, $apiurl);
$jsarray = doLogIn($jsarray, $userName, $password, $apikey, $apiurl);

} else {
echo "userName == '' || password == ''";
}
}
///////////////////////////////////////// FUNCTIONS

function initApiRequest($clientname, $clientversion, $platformname, $platformversion, $uniqueid, $apiurl) {
$init_request = $apiurl .'?api_m=api_init&clientname='. $clientname .'&clientversion='.$clientversion .'&platformname='.$platformname .'&platformversion=' .$platformversion .'&uniqueid='.$uniqueid;
$jsarray = doRestRequest($init_request);
return $jsarray;
}

function doLogIn($jsarray, $userName, $password, $apikey, $apiurl) {
$requestparams = array('api_m' => 'login_login', 'vb_login_username' => $userName, 'vb_login_md5password' => md5($password));
ksort($requestparams);

$login_string = http_build_query($requestparams, '', '&');

$apiaccesstoken = urlencode($jsarray->{'apiaccesstoken'});
$apiclientid = urlencode($jsarray->{'apiclientid'});
$secret = urlencode($jsarray->{'secret'});

$apiversion = urlencode($jsarray->{'apiversion'});

$api_sig = urlencode(md5($login_string . $apiaccesstoken . $apiclientid . $secret . $apikey));
$api_registration_information = 'api_c='. $apiclientid .'&api_s='. $apiaccesstoken .'&api_sig='. $api_sig .'&api_v='. $apiversion;
$login_request = '&'.$api_registration_information.'&vb_login_usern ame='.$userName.'&vb_login_md5password='.md5($pass word);
$url = $apiurl .'?api_m=login_login';
$jsarray = doRestRequests($login_request, $url);
return $jsarray;
}

function doRestRequest($restRequest) {
echo "<br>DO REST REQUEST[". $restRequest ."]<br>";

$content = '';
//Open and read the content
$fp = fopen($restRequest, 'r');

// keep reading until there's nothing left
while ($line = fread($fp, 1024)) {
$content .= $line;
}
fclose($fp);

echo "<hr>";
var_dump(json_decode($content, true));
echo "<hr>";

//decode the content
$jsarray = json_decode($content);
return $jsarray;
}

function doRestRequests($restRequest, $url) {
echo "<br>DO REST REQUEST[". $restRequest ."]<br>";
$context_options = array (
'http' => array (
'method' => 'POST',
'header'=> "Content-type: application/x-www-form-urlencoded",
'content' => $restRequest
)
);
$context = stream_context_create($context_options);

$content = '';
//Open and read the content
$fp = fopen($url.$restRequest, 'r', false, $context);

// keep reading until there's nothing left
while ($line = fread($fp, 1024)) {
$content .= $line;
}
fclose($fp);

echo "<hr>";
var_dump(json_decode($content, true));
echo "<hr>";

//decode the content
$jsarray = json_decode($content);
return $jsarray;
}
?>
<html>
<head>
</head>
<body>
<form action="#" method="post">
<p>Username: <input type="text" name="username" /></p>
<p>Password: <input type="password" name="password" /></p>
<p><input type="submit" /></p>
</form>
</body>
</html>
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 11:53 PM.


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.03233 seconds
  • Memory Usage 2,170KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)post_thanks_box
  • (1)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit_info
  • (1)postbit
  • (1)postbit_onlinestatus
  • (1)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.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
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete