Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mahmoudtrageh's avatar

Uncaught SyntaxError: Unexpected end of JSON input

this function in cpanel api package and give this error

 public function createSubDomain($subdomain, $rootdomain, $dir)
{
    $module = "SubDomain";
    $function = "addsubdomain";
    $parameters = array(
        'domain'        => $subdomain,
        'rootdomain'    => $rootdomain,
        'canoff'        => 0,
        'dir'           => $dir,
        'disallowdot'   => 0
    );
    
    return $this->call($module, $function, $parameters);
}
		

public function call($module, $function, $args = array())
{
  
    $parameters = '';
    if ( count($args) > 0 ) {
        foreach( $args as $key => $value ) {
            $parameters .= '&' . $key . '=' . $value;
        }
    }

    $url = $this->protocol.'://'.$this->domain . ':' . $this->port . '/execute/' . $module;
    $url .= "/".$function;
   
    if(count($args) > 0)
    {
        $url .= '?'. $parameters;
    }

    $headers = array(
        "Authorization: cpanel " . $this->username . ':' . $this->token,
        "cache-control: no-cache"
    );

    $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_PORT => $this->port,
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "GET",
        CURLOPT_POSTFIELDS => "",
        CURLOPT_HTTPHEADER => $headers,
    ));

    $response = curl_exec($curl);
    
    $err = curl_error($curl);

    curl_close($curl);


    if ($err) {

        $response['status'] = 'failed';
        $response['errors'] = $err;
        $response['inputs']['url'] = $url;

    } else {

        $res = json_encode($response);

        $response = [];
        if(isset($res) && isset($res->status) && $res->status == 0)
        {
            $response['status'] = 'failed';
            $response['errors'][] = $res->errors;
            $response['inputs']['url'] = $url;
        } else
        {
           
            $response['status'] = 'success';
            $response['data'] = $res;
            $response['inputs']['url'] = $url;
        }
    }

    return $response;
}
0 likes
1 reply
rodrigo.pedra's avatar

I guess you need to use json_decode() instead of json_encode() in the else block after the curl request

Please or to participate in this conversation.