jonijoba
10-15-2017, 11:44 PM
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/blob/master/core/bridges/vbulletin.bridge.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::processNewLogin($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 1508118515 at 1508118515 ---------------
This script only works with internal use,
I mean inside the forum root directory.
--------------- Added 1508170351 at 1508170351 ---------------
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/archive/index.php/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?api_m=login_login&api_c=2&api_api_sig=75f4b7604f3baa44a24afcdd67d8137f&api_v=525&vb_login_username=jonijoba&vb_login_md5password=4297f44b13955235245b2497399d7 a93&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_username='.$userName.'&vb_login_md5password='.md5($password);
$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>
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/blob/master/core/bridges/vbulletin.bridge.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::processNewLogin($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 1508118515 at 1508118515 ---------------
This script only works with internal use,
I mean inside the forum root directory.
--------------- Added 1508170351 at 1508170351 ---------------
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/archive/index.php/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?api_m=login_login&api_c=2&api_api_sig=75f4b7604f3baa44a24afcdd67d8137f&api_v=525&vb_login_username=jonijoba&vb_login_md5password=4297f44b13955235245b2497399d7 a93&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_username='.$userName.'&vb_login_md5password='.md5($password);
$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>