The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

cPanel API failure questions

Discussion in 'cPanel Developers' started by gunsuka, Jun 24, 2015.

  1. gunsuka

    gunsuka Member

    Joined:
    Jan 18, 2015
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    I built some code that runs and connects to various cPanel servers - all at different ISP's to pull down email info for a spam protection system.

    Here is a snippet of code:

    PHP:
    try {

       if ( 
    $jResult $cPanel->api2_query($userAccount"Email""listpops"$domain ) ) {

         if ( 
    $objResult json_decode($jResult)  ) {

           
    // Says the result was success, lets check the JSON and see if it really was a success.
          
         
    } else {

           
    // Some error, searching in $jResult which is probably an HTML result like 401 access denied

         
    }

       } else {

         
    // WOW PROBLEM.  If we end up here $jResult is totally empty, how do we get debug info

       
    }

    } catch ( 
    Exception $e) {

       
    // Handle exceptions, things like unable to resolve host, connection failures etc. end up here


    }
    I have quite a bit of error handling to deal with the problems I see. Connection errors, unable to resolve host, access denied etc. They are all caught with these different handling routines.

    You can see WOW PROBLEM, I end up here sometimes and $jResult is nothing. I have nothing returned from the server, no error of any type - $jResult is blank. How can I get something to determine what is going on?

    I suspect I end up here under at least a couple situations based on manual testing.

    1) If I connect to port 2082 and the system redirects me to port 2083, I end up in the WOW PROBLEM section.

    2) The server has a self signed SSL cert, I think I also end up in the WOW PROBLEM section of code. Not sure of these two cases, because I don't know where to look for any error information.

    Any ideas?
     
  2. gunsuka

    gunsuka Member

    Joined:
    Jan 18, 2015
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Thought I would put an update here for the benefit of others.

    I am using the PHP XML API client class, verion 1.0.13.

    I found it has the following code:

    PHP:
    // The only time a response should contain <html> is in the case of authentication error
    // cPanel 11.25 fixes this issue, but if <html> is in the response, we'll error out.

    if (stristr($response[0], '<html>') == true) {
      if (
    stristr($response[0], 'Login Attempt Failed') == true) {
       
    error_log("Login Attempt Failed");

       return;
      }
      if (
    stristr($response[0], 'action="/login/"') == true) {
       
    error_log("Authentication Error");

       return;
      }

      return;
    }
    It was essentially assuming that if a reply every contained an <HTML> tag that it was a failed login.

    I've found that if you try and connect to a cPanel server on port 2082 but it wants a secure connection on port 2083 that it will return a 301 Location: header, along with a meta refresh tag... and it contains the <html> tag.

    I ended up making a couple of modifications to the API client class, to remove the above check and also rather than just returning the body that curl gets it will also return to me all headers returned by the remote server. So I have been able to increase my error handling by recognizing 301 Location redirects, 404 file not founds, 500 server class errors etc.

    Since I operate in not a very controlled environment (user's supply their cPanel URL's) we get all types of things sent to us that are not correct so having access to as much data like headers is very important for us.
     
Loading...

Share This Page