Неустранимая ошибка: необработанная ошибка: вызов неопределенной функции app_create ()

Я пытаюсь использовать serverPilot API с моего сайта. Я создал простые функции, как показано ниже, для примера использования, но это дает мне ошибку, как показано ниже

Fatal error: Uncaught Error: Call to undefined function app_create()

Я новичок в PHP и не знаю, как правильно объявлять и использовать функции. Дайте мне знать, что мне не хватает в этом? Мой полный код PHP, как показано ниже

<?php

if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['createApp'])){

$name = "sampleName";
$name = "hello";
$runtime ="php5.5";
$password = "Test@123";
$domains = array("www.example.com","example2.com");
app_create( $name, $sysuserid, $runtime, $domains = array());

}
else if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['createDb'])){

$id = 1;
$name = "hello";
$username ="testuser";
$password = "Test@123";

database_create( $id, $name, $username, $password );

}
else if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['createUser'])){

$id = 1;
$name = "hello";
$password = "Test@123";

sysuser_create( $id, $name, $password = NULL )();
}




class ServerPilot {
// variables
public $apiID = "";
public $apiKey = "";
public $decode;
// constants
const SP_API_ENDPOINT       = 'https://api.serverpilot.io/v1/';
const SP_USERAGENT          = 'ServerPilot-PHP/1.0';
const SP_HTTP_METHOD_POST   = 'post';
const SP_HTTP_METHOD_GET    = 'get';
const SP_HTTP_METHOD_DELETE = 'delete';
// error constants
const SP_MISSING_CONFIG = 'Missing config data';
const SP_MISSING_API    = 'You must provide API credentials';
const SP_CURL_ERROR     = 'Curl error code returned ';

public function __construct( $config = array() ) {
if( empty($config) ) throw new Exception(ServerPilot::SP_MISSING_CONFIG);
if( !isset($config['id']) || !isset($config['key']) ) throw new Exception(ServerPilot::SP_MISSING_API);
$this->apiID    = $config['id'];
$this->apiKey   = $config['key'];
$this->decode   = ( isset($config['decode']) ) ? $config['decode'] : true;
}

public function sysuser_create( $id, $name, $password = NULL ) {
$params = array(
'serverid'  => $id,
'name'      => $name);
if( $password )
$params['password'] = $password;
return $this->_send_request( 'sysusers', $params, ServerPilot::SP_HTTP_METHOD_POST );
}

public function app_create( $name, $sysuserid, $runtime, $domains = array() ) {
$params = array(
'name'      => $name,
'sysuserid' => $sysuserid,
'runtime'   => $runtime);
if( $domains )
$params['domains'] = $domains;

return $this->_send_request( 'apps', $params, ServerPilot::SP_HTTP_METHOD_POST );
}


public function database_create( $id, $name, $username, $password ) {
$user = new stdClass();
$user->name = $username;
$user->password = $password;
$params = array(
'appid'     => $id,
'name'      => $name,
'user'      => $user);
return $this->_send_request( 'dbs', $params, ServerPilot::SP_HTTP_METHOD_POST );
}

private function _send_request( $url_segs, $params = array(), $http_method = 'get' )
{
// Initialize and configure the request
$req = curl_init( ServerPilot::SP_API_ENDPOINT.$url_segs );
curl_setopt( $req, CURLOPT_USERAGENT, ServerPilot::SP_USERAGENT );
curl_setopt( $req, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
curl_setopt( $req, CURLOPT_USERPWD, $this->apiID.':'.$this->apiKey );
curl_setopt( $req, CURLOPT_RETURNTRANSFER, TRUE );
// Are we using POST or DELETE? Adjust the request accordingly
if( $http_method == ServerPilot::SP_HTTP_METHOD_POST ) {
curl_setopt( $req, CURLOPT_HTTPHEADER, array('Content-Type: application/json') );
curl_setopt( $req, CURLOPT_POST, TRUE );
curl_setopt( $req, CURLOPT_POSTFIELDS, json_encode($params) );
}
if( $http_method == ServerPilot::SP_HTTP_METHOD_DELETE ) {
curl_setopt( $req, CURLOPT_CUSTOMREQUEST, "DELETE" );
}
// Get the response, clean the request and return the data
$response = curl_exec( $req );
$http_status = curl_getinfo( $req, CURLINFO_HTTP_CODE );
curl_close( $req );
// Everything when fine
if( $http_status == 200 )
{
// Decode JSON by default
if( $this->decode )
return json_decode( $response );
else
return $response;
}
// Some error occurred
$data = json_decode( $response );
// The error was provided by serverpilot
if( property_exists( $data, 'error' ) && property_exists( $data->error, 'message' ) )
throw new ServerPilotException($data->error->message, $http_status);
// No error as provided, pick a default
switch( $http_status )
{
case 400:
throw new ServerPilotException('We couldn\'t understand your request. Typically missing a parameter or header.', $http_status);
break;
case 401:
throw new ServerPilotException('Either no authentication credentials were provided or they are invalid.', $http_status);
break;
case 402:
throw new ServerPilotException('Method is restricted to users on the Coach or Business plan.', $http_status);
break;
case 403:
throw new ServerPilotException('Forbidden.', $http_status);
break;
case 404:
throw new ServerPilotException('You requested a resource that does not exist.', $http_status);
break;
case 409:
throw new ServerPilotException('Typically when trying creating a resource that already exists.', $http_status);
break;
case 500:
throw new ServerPilotException('Something unexpected happened on ServerPilot\'s end.', $http_status);
break;
default:
throw new ServerPilotException('Unknown error.', $http_status);
break;
}
}
}
?>

<html>
<body>

<form action="server.php" method="post">
<input type="submit" name="createApp" value="Create APP" />
</form>
</br>
<form action="server.php" method="post">
<input type="submit" name="createDb" value="Create DB" />
</form>
</br>
<form action="server.php" method="post">
<input type="submit" name="createUser" value="Create USER" />
</form>

</body>
</html>

Его ошибка во всех трех функциях одинакова. Дайте мне знать, если кто-то может помочь мне выйти из этой проблемы, я пытаюсь последние два часа, и это не работает.
Спасибо

0

Решение

Вы не можете получить доступ к функции класса напрямую, как это. Вам нужно сначала создать объект класса, а затем вызвать функцию с переменной объекта.

Чтобы избежать ошибок, лучше сохранить код класса в отдельном файле и включить его в приведенный выше файл.

Например:

    // $config as array, You need it to set in construct method.check construct method.
$config = [
"id" => ENTER_ID,
"key" => ENTER_KEY,
"decode" => true, //Optional you can leave this ,default is true anyway.
];
$ServerPilot_Obj = New ServerPilot($config);
$ServerPilot_Obj->app_create( $name, $sysuserid, $runtime, $domains = array());
1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector