致命错误:未捕获错误:调用未定义的函数app_create()

时间:2018-12-12 05:38:36

标签: php

我正在尝试从我的网站使用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>

在所有三个函数中都给出错误。让我知道是否有人可以帮助我解决此问题,我从最近两个小时开始尝试,但仍无法正常工作。 谢谢

1 个答案:

答案 0 :(得分:1)

不能像这样直接访问类函数。需要先创建类对象,然后使用对象变量调用该函数。

最好将类代码保存在单独的文件中,并将其包含在上面的gift文件顶部,以免出错。

例如:

    // $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());