PHP Cpanel_PublicAPI Create Account passing incorrect variables - php

I am sorry if this request is in the wrong area.
I am learning to use PHP and the CPanel API. I've got the following code and it's giving me a stack error because I am missing something seemingly simple
$domain = array('username' => 'bobbie', 'domain' => 'bobbie.com', 'pass' => 'bobbie123');
$acct = $cp->whm_api('createacct', $domain);
echo "WHM Create: {$acct->createacct}\n";
I know I'm connecting to WHM properly because my code before this outputs the version of WHM correctly. The above code is giving me an error stating that createacct needs to be passed an array as the first parameter:
WHM Version: 11.54.0.21
PHP Fatal error: Uncaught exception 'Exception' with message 'createacct requires that first parameter passed to it is an array' in /root/whmrm/Cpanel/Service/XmlapiClientClass.php:146
Stack trace:
#0 [internal function]: Cpanel_Service_XmlapiClientClass->createacct('bobbie', 'bobbie.com', 'bobbie123')
#1 /root/whmrm/Cpanel/Service/WHM.php(195): call_user_func_array(Array, Array)
#2 [internal function]: Cpanel_Service_WHM->__call('createacct', Array)
#3 [internal function]: Cpanel_Service_WHM->createacct('bobbie', 'bobbie.com', 'bobbie123')
#4 /root/whmrm/Cpanel/PublicAPI.php(525): call_user_func_array(Array, Array)
#5 /root/whmrm/create_sites_on_server.php(68): Cpanel_PublicAPI->__call('whm_api', Array)
#6 /root/whmrm/create_sites_on_server.php(68): Cpanel_PublicAPI->whm_api('createacct', Array)
#7 {main}
thrown in /root/whmrm/Cpanel/Service/XmlapiClientClass.php on line 146
Line 3 of the output is showing that I'm not sending the data properly. Any help would be appreciated. I've googled and most of the results give me information about the xml_api and how to use that. Thanks for your assistance.

Looking at the source for the PublicAPI class, it appears that if an array is given as the parameters, it only calls the resulting function passing the first element from the array (source).
I was able to create an account using this code:
$cp = Cpanel_PublicAPI::getInstance($config);
$whm = Cpanel_PublicAPI::factory('whm');
$domain = array(
'domain' => 'mydomain.com',
'username' => 'drewt2',
'password' => 'myp4ssw0rd!'
);
$response = $whm->createacct($domain);
You can see the functions and there parameters here: Cpanel_Service_XmlapiClientClass.
Unfortunately, the code hasn't been updated in 5 years, and the examples aren't that helpful so you'll likely have to look through the code to figure out most of what you'll want to do.

Related

Yii2 REST API not working properly

I am creating an API for get user by YII2 user api by user id. If I pass user id then it is working good but If I pass string then I get below error.
<pre>An Error occurred while handling another error:
yii\base\InvalidParamException: Response content must not be an array. in /var/www/html/vendor/yiisoft/yii2/web/Response.php:1020
Stack trace:
#0 /var/www/html/vendor/yiisoft/yii2/web/Response.php(313): yii\web\Response->prepare()
#1 /var/www/html/vendor/yiisoft/yii2/web/ErrorHandler.php(128): yii\web\Response->send()
#2 /var/www/html/vendor/yiisoft/yii2/base/ErrorHandler.php(111): yii\web\ErrorHandler->renderException(Object(yii\web\NotFoundHttpException))
#3 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\web\NotFoundHttpException))
#4 {main}
Previous exception:
yii\base\InvalidRouteException: Unable to resolve the request: categories/wewqewqe in /var/www/html/vendor/yiisoft/yii2/base/Controller.php:127
Stack trace:
#0 /var/www/html/vendor/yiisoft/yii2/base/Module.php(523): yii\base\Controller->runAction('wewqewqe', Array)
#1 /var/www/html/vendor/yiisoft/yii2/web/Application.php(102): yii\base\Module->runAction('categories/wewq...', Array)
#2 /var/www/html/vendor/yiisoft/yii2/base/Application.php(380): yii\web\Application->handleRequest(Object(yii\web\Request))
#3 /var/www/html/index.php(16): yii\base\Application->run()
#4 {main}
Next yii\web\NotFoundHttpException: Page not found. in /var/www/html/vendor/yiisoft/yii2/web/Application.php:114
Stack trace:
#0 /var/www/html/vendor/yiisoft/yii2/base/Application.php(380): yii\web\Application->handleRequest(Object(yii\web\Request))
#1 /var/www/html/index.php(16): yii\base\Application->run()
#2 {main}</pre>
I am using swagger for executing APIs and Endpoint of the API is
users/{id}
Work fine when enter
/users/1
Get error when
/users/asdf
Web.php
'users/<id:\d+>' => 'users/',
I have recently faced same issue and it has been solved by the change in routing in web.php in config folder.
Change your url
users/<id> instead of users/<id:\d+>.

AnchorCMS installation fails

When I've gone through the installation steps for Anchor CMS, I'm getting an error (after final step):
Uncaught Exception
Undefined variable: vars
Origin
install/routes.php on line 183
Trace
#0 /home/.../anchor-cms/install/routes.php(183): System\error::native(8, 'Undefined varia...', '/home/...', 183, Array)
#1 [internal function]: {closure}()
#2 /home/.../anchor-cms/system/route.php(165): call_user_func_array(Object(Closure), Array)
#3 /home/.../anchor-cms/system/router.php(158): System\route->run()
#4 /home/.../anchor-cms/system/start.php(46): System\router->dispatch()
#5 /home/.../anchor-cms/install/index.php(33): require('/home/...')
#6 {main}
Any suggestions? I'm running Linux 64bit (elementaryOS).
You may have already resolved this, but the way I got around this was to remove the undefined variable from line 183 of the install/routes.php file completely.
return Layout::create('account' $vars);
So it then looked like this:
return Layout::create('account');
Refreshing the page will then allow you to create an account and login to the admin interface etc.

RedBean 4.3 "A database has already be specified for this key"

I making a small site and decided to use Valitron library for the validation part of my site. I noticed there is no kind of "unique" method that returns whether a passed value already exists in the database or not. I quickly added this method, but not like it is recommended to do. I added this:
protected function validateUnique($field, $value, $table)
{
$model = new Model();
return $model->unique($table[0], $field, $value);
}
to the main Validator class. The thing is, it is working, but only with one field. If I use this on 2 or more fields in my validation proccess, I get this error
Fatal error: Uncaught exception 'RedBeanPHP\RedException' with message 'A database has already be specified for this key.' in C:\dev\htdocs\Shop\vendor\gabordemooij\redbean\RedBeanPHP\Facade.php:295 Stack trace: #0 C:\dev\htdocs\Shop\vendor\gabordemooij\redbean\RedBeanPHP\Facade.php(216): RedBeanPHP\Facade::addDatabase('default', 'mysql:host=127....', 'root', '', false) #1 C:\dev\htdocs\Shop\app\libs\Model.php(18): RedBeanPHP\Facade::setup('mysql:host=127....', 'root', '') #2 C:\dev\htdocs\Shop\vendor\vlucas\valitron\src\Valitron\Validator.php(429): Shop\libs\Model->__construct() #3 [internal function]: Valitron\Validator->validateUnique('email', 'email.example#g...', Array) #4 C:\dev\htdocs\Shop\vendor\vlucas\valitron\src\Valitron\Validator.php(885): call_user_func(Array, 'email', 'email.example#g...', Array) #5 C:\dev\htdocs\Shop\app\controllers\AuthController.php(68): Valitron\Validator->validate() #6 [internal function]: Shop\controllers\AuthController->postRegister() #7 C:\dev\htdocs\Shop\app\libs\Bootstrap.php(64): in C:\dev\htdocs\Shop\vendor\gabordemooij\redbean\RedBeanPHP\Facade.php on line 295
I work with RedBean as well as Valitron for the first time and I'm not able to say what causes the problem.
I got the same error.
A database has already be specified for this key. in /var/www/html...
I have already setup one database and want to connect another database then I used R::addDatabase() function to add new databse and R::selectDatabase() function to select that database.
code is
define('dsn2', 'pgsql:host=localhost;dbname=mydb';
R::addDatabase('DB2', dsn2, 'dbusers', 'dbpasss');
R::selectDatabase('DB2');

Zf2 "The iterator class does not exist" on Session\Container definition

Sometimes on my website (ZendFramework2), I get an error when setting a session variable.
The error:
PHP Fatal error: Uncaught exception 'Zend\\Stdlib\\Exception\\InvalidArgumentException' with message 'The iterator class does not exist' in /MySite/vendor/zendframework/zendframework/library/Zend/Stdlib/ArrayObject/PhpReferenceCompatibility.php:374
Stack trace:
#0 /MySite/vendor/zendframework/zendframework/library/Zend/Stdlib/ArrayObject/PhpReferenceCompatibility.php(414): Zend\\Stdlib\\ArrayObject\\PhpReferenceCompatibility->setIteratorClass(NULL)
#1 [internal function]: Zend\\Stdlib\\ArrayObject\\PhpReferenceCompatibility->unserialize('a:4:{s:7:"stora...')
#2 /MySite/vendor/zendframework/zendframework/library/Zend/Session/SessionManager.php(95): session_start()
#3 /MySite/vendor/zendframework/zendframework/library/Zend/Session/AbstractContainer.php(78): Zend\\Session\\SessionManager->start()
#4 /MySite/module/Application/Module.php(97): Zend\\Session\\AbstractContainer->__construct('site')
#5 [internal function]: Application\\Module->onBootstrap(Object(Zend\\Mvc\\MvcEvent))
#6 /MySite/vendor/zendframework/zendframework/library/Zend/Stdlib/ArrayObject/PhpReferenceCompatibility.php on line 374
Code Module.php line 97 :
$s_site = new \Zend\Session\Container('site');
I try but I can not find why. Do you have any idea? Need more informations?
Thank you.
The is a result of the setIteratorClass method in ArrayObject/PhpReferenceCompatibility being passed a null $class argument. I'm not sure why it sometimes gets a null, but the way I fixed it in my case was to set the iterator class as 'ArrayIterator' when passed a null object, like this:
if (!isset($class)) {
$class = 'ArrayIterator';
}
In the latest zf2 version, the ArrayObject/PhpReferenceCompatibility class has been removed, so please make the change in stdlib/ArrayObject.php.

unable to post using curl in laravel

I am leaning laravel. I am working on Rest API. I have made the controller to post using curl as follow
public function post_index()
{
$cat = Input::json();
$dbCat = new Category();
$dbCat->code = $cat->code;
$dbCat->name = $cat->name;
$dbCat->save();
return $dbCat->toJson();
}
when I am trying to post using curl then i am getting following error
Message:
Trying to get property of non-object
Location:
C:\xampp\htdocs\NewBlog\application\controllers\api\category.php on line 22
and the stack trace is
#0 C:\xampp\htdocs\NewBlog\laravel\laravel.php(42): Laravel\Error::native(8, 'Trying to get p...', 'C:\xampp\htdocs...', 22)
#1 C:\xampp\htdocs\NewBlog\application\controllers\api\category.php(22): Laravel\{closure}(8, 'Trying to get p...', 'C:\xampp\htdocs...', 22, Array)
#2 [internal function]: Api_Category_Controller->post_index()
#3 C:\xampp\htdocs\NewBlog\laravel\routing\controller.php(325): call_user_func_array(Array, Array)
#4 C:\xampp\htdocs\NewBlog\laravel\routing\controller.php(285): Laravel\Routing\Controller->response('index', Array)
#5 C:\xampp\htdocs\NewBlog\laravel\routing\controller.php(165): Laravel\Routing\Controller->execute('index', Array)
#6 C:\xampp\htdocs\NewBlog\laravel\routing\route.php(153): Laravel\Routing\Controller::call('api.category#in...', Array)
#7 C:\xampp\htdocs\NewBlog\laravel\routing\route.php(124): Laravel\Routing\Route->response()
#8 C:\xampp\htdocs\NewBlog\laravel\laravel.php(167): Laravel\Routing\Route->call()
#9 C:\xampp\htdocs\NewBlog\public\index.php(34): require('C:\xampp\htdocs...')
#10 {main}
how can I come out through this problem ?
Since you are filling $cat with Input::json() you'll need to make sure that you are sending a curl request that has a header that is setting the content type to application/json
curl isn't really built for sending json, are you able to send normal post fields instead?
Really... we need more information on your situation, like the code you are using to send a curl request
Without knowing what line 22 is, my suggestion would be to check that $cat is actually an object. It's most likely throwing that error because it's null or, as the error says, a non-object.
If it is working as indicated in the other answer, and you are now encountering a problem with a "doubling up" of the json wrapper then I would suggest using eloquent_to_json() instead of json_encode()
http://laravel.com/api/function-eloquent_to_json.html

Categories