使用API​​

时间:2015-07-13 16:09:53

标签: php api single-sign-on cpanel whm

真的很难与这个人挣扎。我正在尝试编写一个PHP脚本来验证cPanel帐户并将用户登录。

如果有人可以查看下面的代码,让我知道我哪里出错了,我将非常感激......

if(isset($_GET['account'])) {

        $cpanel_user = $_GET['account'];

        $query = "https://whm.brixly.uk:2087/json-api/create_user_session?api.version=1&user=$cpanel_user&service=cpaneld";

        $curl = curl_init();                                     // Create Curl Object.
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);       // Allow self-signed certificates...
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);       // and certificates that don't match the hostname.
        curl_setopt($curl, CURLOPT_HEADER, false);               // Do not include header in output
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);        // Return contents of transfer on curl_exec.
        $header[0] = "Authorization: Basic " . base64_encode($reseller_username.":".$reseller_password) . "\n\r";
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);         // Set the username and password.
        curl_setopt($curl, CURLOPT_URL, $query);                 // Execute the query.
        $result = curl_exec($curl);
        if ($result == false) {
            error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");
                                                            // log error if curl exec fails
        }
        $decoded_response = json_decode( $result, true );

        $session_url = $decoded_response['data']['url'];
        $cookie_jar = 'cookie.txt';

        curl_setopt($curl, CURLOPT_HTTPHEADER, null);             // Unset the authentication header.
        curl_setopt($curl, CURLOPT_COOKIESESSION, true);          // Initiate a new cookie session.
        curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);       // Set the cookie jar.
        curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_jar);      // Set the cookie file.
        curl_setopt($curl, CURLOPT_URL, $session_url);            // Set the query url to the session login url.

        $result = curl_exec($curl);                               // Execute the session login call.
        if ($result == false) {
            error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");
                                                            // Log an error if curl_exec fails.
        }
        $session_url = preg_replace( '{/login(?:/)??.*}', '', $session_url );  // make $session_url = https://10.0.0.1/$session_key
        $query = "$session_url/execute/Ftp/list_ftp";

        curl_setopt($curl, CURLOPT_URL, $query);  // Change the query url to use the UAPI call.
        $result = curl_exec($curl);               // Execute the UAPI call.
        if ($result == false) {
            error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");    // log error if curl exec fails
        }
        curl_close($curl);
        header("Location: $query");
        print $result;

    }

而不是上面的代码登录,它只是重定向到cPanel登录页面。

我传递的所有变量都是正确的,因为URL正在返回。据我所知,该URL需要随后发布,以便设置cookie,从而验证会话。

非常感谢,

丹尼斯

1 个答案:

答案 0 :(得分:0)

之前我也使用过这个脚本,但现在它根本不起作用 - 也许cPanel更新导致了这个问题。 :(如果可能,请尝试使用"Username and password authentication"

<?php

    $query = "https://whm.brixly.uk:2087/json-api/listaccts?api.version=1";

    $curl = curl_init();                                 // Create Curl Object
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);       // Allow self-signed certs
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);       // Allow certs that do not match the hostname
    curl_setopt($curl, CURLOPT_HEADER, 0);               // Do not include header in output
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);       // Return contents of transfer on curl_exec
    $header[0] = "Authorization: Basic " . base64_encode($reseller_username.":".$reseller_password) . "\n\r";
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);     // set the username and password
    curl_setopt($curl, CURLOPT_URL, $query);             // execute the query
    $result = curl_exec($curl);
    if ($result == false) {
        error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query"); 
                                                         // log error if curl exec fails
    }
    curl_close($curl);
 
    print $result;

?>