problem fetching the env variablecontaining the path to the socket

JAB Creations

Active Member
Nov 21, 2009
44
7
58
Using the following:
PHP:
<?php
include('/__fake_path__/cpanel.php');

try
{
 $cpanel = new CPANEL();
}
catch (Exception $e)
{
   echo 'Caught exception: ',  $e->getMessage(), "\n";
}
?>
I received the following error:

Caught exception: There was a problem fetching the env variablecontaining the path to the socket
I read this post which did not make sense and tried the command on this page dotcult.com/what-the-hell-is-cpsrvd/ which had no effect.

I have never actually gotten this cPanel class to work and have only had limited success with $xmlapi->api2_query. My only goal is to simply create an email account via PHP. If there is some higher level knowledge, permissions, etc that someone without a lot of experience using APIs should be made aware of please include those details.
 
Last edited by a moderator:

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,258
463
My only goal is to simply create an email account via PHP. If there is some higher level knowledge, permissions, etc that someone without a lot of experience using APIs should be made aware of please include those details.
Hello,

Is this for a cPanel plugin you are developing (that will appear for users in the cPanel UI), or is this for a standalone PHP script you are creating? If you are simply wanting to create an email account via a PHP script, there's no need to use the LiveAPI system. You could follow an example like the one referenced in the following thread:

How to make new email accounts for cPanel user, from PHP script

Thank you.
 

JAB Creations

Active Member
Nov 21, 2009
44
7
58
Thank you for the reply Michael. I'm trying to get this to work via PHP (tried the XML API first though abandoned it because the Github project looks largely abandoned with many pending commits).

Using this:

PHP:
$result = $xmlapi->api2_query(
    'Email', 'addpop',
    array(
        'domain'          => 'example.com',
        'email'           => 'username',
        'password'        => 'password',
        'quota'           => '0',
    )
);
I get the following error:

SimpleXMLElement Object
(
[error] => User parameter is invalid or was not supplied
[data] => SimpleXMLElement Object
(
[result] => 0
[reason] => User parameter is invalid or was not supplied
)
)
I changed email field to user and still received the same error. What do I need to adjust?
 

JAB Creations

Active Member
Nov 21, 2009
44
7
58
I was missing the very first parameter: the user string (the cPanel username).

PHP:
 $result = $xmlapi->api2_query('cpanel_user_name','Email', 'addpop', array('domain' => 'example.com', 'email' => 'username', 'password' => 'password', 'quota' => '0'));
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,258
463
I was missing the very first parameter: the user string (the cPanel username).
Can you verify if that solved the issue?

tried the XML API first though abandoned it because the Github project looks largely abandoned with many pending commits
cPanel version 70 will be the last version to support XML-like output of our API. Here's a quote from our March 2017 Development Update:

XML-like output of APIs
The XML-like output of our API (often called the XML API) is already officially deprecated, and cPanel & WHM Version 70 will be the last version to support this output. Integrators and API users will want to begin switching to use the JSON output of the API now. The XLM-like format has long caused problems for integrators and developers because it is not valid XML, and we find that removing it completely will help reduce that.
Thank you.
 

JAB Creations

Active Member
Nov 21, 2009
44
7
58
Can you verify if that solved the issue?
Yes.

Why did you recommend the XML version if it was not only deprecated though also being outright removed in March?

Integrators and API users will want to begin switching to use the JSON output of the API now.
How? Is this something I trigger in the code or do I have to upload another file that I am not currently aware of? Where is that JSON equivalent? I highly prefer JSON for handling things on the server any way. Also I do not want to deal with having to run scripts in some highly miscellaneous unrelated directory. The development update did not mention an alternative.
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,258
463
Why did you recommend the XML version if it was not only deprecated though also being outright removed in March?
Hello,

I've modified the response so that it no longer reflects the link to the PHP client class for interacting with cPanel's XML-API. It's a useful resource for proof-of-concept examples, but you are correct that it's not a good idea to offer that resource with the impending removal of support for XML-like output of APIs.

How? Is this something I trigger in the code or do I have to upload another file that I am not currently aware of? Where is that JSON equivalent? I highly prefer JSON for handling things on the server any way. Also I do not want to deal with having to run scripts in some highly miscellaneous unrelated directory. The development update did not mention an alternative.
Essentially, if your custom scripts are designed to rely on the XML-like output of our API functions, then you should modify them to instead rely on the JSON output. Here are a couple of threads that offer some insight into the differences:

SOLVED - Manage Json Result from API
WHM xmlApi call UAPI functions

Thank you.
 

JAB Creations

Active Member
Nov 21, 2009
44
7
58
None of this helps, it's all fragmented and there are no full examples with basic obfuscation.

Reading the UAPI documentation (which apparently should be called API3) it says to first do the following:

PHP:
$cpanel = new CPANEL();
Which spawns the following error:

Fatal error: Uncaught Error: Class 'CPANEL' not found
For which there is little to nothing I could find in the search engines using quote operators. It's absolutely buried (four pages minimum to find) but it is listed here:

Guide to the LiveAPI System - PHP Class - Software Development Kit - cPanel Documentation
Which spawns the following error:

<b>Fatal error</b>: Uncaught RuntimeException: There was a problem fetching the env variablecontaining the path to the socket in /usr/local/cpanel/php/cpanel.php:146
So now I'm right back at my first post!

So I go digging and that line in the file at /usr/local/cpanel/php/cpanel.php refers to
Code:
getenv('CPANEL_PHPCONNECT_SOCKET')
which when I echo that from an example.live.php file located at /usr/local/cpanel/base/frontend is apparently blank.

A reference here suggests that I can not call the PHP function from a live page (even included?)? Then they suggest to use the JSON version of the XML API. BUT YOU JUST SAID TO USE THE XML API THAT IS GOING TO BE DEPRECATED! This is all a circulatory waste of time!

So absolutely dead-simple:

If I have a PHP script located at:

/home/_user_/public_html/example.php

How do I have that PHP script create a new mail account when called from the client browser that will not expire after a lousy month or two?
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,258
463
Hello,

The LiveAPI system provides environments for applications in the cPanel interface. Thus, you should not use the LiveAPI system because your script exists within an account, and you are not intending it for use in the cPanel interface (e.g. you are not making a plugin that a user will access within their cPanel interface).

There's no specific example of a PHP script that utilizes the email creation UAPI function. This will require custom coding and development on your part. That said, you may find user-submitted examples like the one referenced on the following third-party URL helpful:

Create a new subdomain and MySQL database via WHM or cPanel API with PHP

Otherwise, consider consulting with a developer or system administrator for help setting up a custom script:

System Administration Services | cPanel Forums

Thank you.