Search code examples
laravellaravel-10laravel-http-clientcpanel-uapi

I need to send an http request from Laravel 10 to cPanel UAPI to create a new email


According to the cPanel documentation:

To call a UAPI function with an API token, run the following command from the command line:

curl -H'Authorization: cpanel username:APITOKEN' 'https://example.com:2083/execute/Module/function?parameter=value'
Item Description Example
username The cPanel account's username. username
APITOKEN The API token. U7HMR63FGY292DQZ4H5BFH16JLYMO01M
example.com Your cPanel server's domain example.com
Module The API module name. Email
function The API function's name. add_pop
parameter The function's input parameters. email
value The value to assign to the input parameter. 12345luggage

For example, your command may resemble the following example:

curl -H'Authorization: cpanel username:U7HMR63FHY282DQZ4H5BIH16JLYSO01M' 'https://example.com:2083/execute/Email/add_pop?email=newuser&password=12345luggage'

I also created the following function in my controller:

public function test()
{
    $response = Http::post('https://example.com:2083' ,[
        'username' => 'holidays',
        'APITOKEN' => 'XPA6V9NSZ5JGKXU5VE2X214U53WROFI0',
        'Module' => 'Email',
        'function' => 'add_pop',
        'parameter' => 'email',
        'value' => 'test',
 
    ]);
    return $response;
    // dd($response);
}

How can I send a request correctly to cPanel to create a new email and get a successful message in Laravel 10?

Thank you


Solution

  • You need to use headers in Laravel HTTP.

    So the correct code will be:

    public function test()
    {
        $response = Http::withHeaders('Authorization', 'cpanel holidays:XPA6V9NSZ5JGKXU5VE2X214U53WROFI0')
            ->post('https://example.com:2083' ,[
                'Module' => 'Email',
                'function' => 'add_pop',
                'parameter' => 'email',
                'value' => 'test',
            ]);
        return $response;
        // dd($response);
    }