doushan2811 2018-12-12 05:38
浏览 242
已采纳

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

I am trying to use serverPilot API from my website. I have created simple functions like below for sample usage but its giving me error like below

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

I am new in PHP and don't know proper method to declare and use functions. Let me know what I am missing in this? My full PHP code is like below

<?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>

Its giving error in all three functions same. Letme know if someone can help me for come out from this issue, I am trying from last two hours and its not working. Thanks

  • 写回答

1条回答 默认 最新

  • dongxiao_0528 2018-12-12 05:47
    关注

    You can not access class function directly like this.You need to create class object first and then call the function with object variable.

    It's better to save class code in a separate file and include it in the above give file at the top to avoid errors.

    E.g:

        // $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());
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?