How can I execute functions in cPanel using cURL? - php

So, I've been trying to execute the function Cron::add_line using an script that I have builded and it didn't work properly yet. The script login into cPanel but in the second part it don't execute the json-api to execute the function add_line, I've did many searches about this but never found anything to make me comprehend how it works in cURL.
If I type the command url in my browser, it works and create the cron, otherwise, using my script it doesn't work.
If anyone could help I appreciate!
Thanks!
Code:
<?php
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$site = 'https://' . $_REQUEST['site'] . ':2083';
$auth_url = $site . '/login/?';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $auth_url);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'user=' . $username . '&pass=' . $password);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100020);
$result = curl_exec($ch);
$h = curl_getinfo($ch);
if ($result == true && strpos($h['url'], "cpsess"))
{
$pattern = "/.*?(\/cpsess.*?)\/.*?/is";
$preg_res = preg_match($pattern, $h['url'], $cpsess);
}
$cpcmd_url = $site . "$ENV{'" . $cpsess[1] . "'}" . '/json-api/';
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $cpcmd_url);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'cpanel?user=$username&cpanel_jsonapi_module=Cron&cpanel_jsonapi_func=add_line&cpanel_jsonapi_version=2&command=GET http://www.cdihost.com.br/admin/cron.php&day=*&hour=*&minute=*/5&month=*&weekday=*');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100020);
$result = curl_exec($ch);
curl_close($ch);
?>

Try some debugging, like error_log($cpcmd_url); to see if the url is correct. Also, in CURLOPT_POSTFIELDS, cpanel? seems wrong to me.

Related

PHP shell_exec get file

Is there any way to get file contents of external webiste, say https://www.example.com/.
For eg. Using Python from shell_exec. I tried python but got blank.
Not sure why you want to use shell_exec but
you can use
$data = file_get_contents('https://www.example.com/');
or
$url = 'https://www.example.com/';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
curl_close($curl);
if you want to maintain the session you could use curl with CURLOPT_COOKIEFILE
eg:
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile");
curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_URL, 'https://www.example.com/');
$data = curl_exec($ch);
or if you really want to use the shell_exec the use follwing
$data = shell_exec('curl -I https://www.example.com/');
echo "<pre>$data</pre>";
however for above to work you have to have curl installed check following for instillation of curl
https://curl.se/
hope this works

How i can send get parameters in PHP Curl?

i tryed to use:
$qry_str = "ft_dl={$ow_ft_dl}&ft_dl_name={$singername} - $songen ({$ow_quality[$i]}).mp3&ft_dl_cover={$src}&sn_name={$singername}&songname={$songen}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, OW_DL_HOST_FUNC . '?' . $qry_str);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_exec($ch);
but when i start the code, i dont have any actions to the target file...
the OW_DL_HOST_FUNC file value like : http://sitename.deve/file.php

php curl removes a session when I parsing the file

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"key=" . $value. " ");
$result = trim(curl_exec($ch));
curl_close($ch);
When I use curl sessions get destroyed. Can you help me?

Login to ssl website with php and cURL

I'm trying to login to Square (squareup.com/login) to retrieve some information about my business and send me an email. However, my ability to make cURL work properly has not made any progress for several days so I'm asking for help.
Here's my code:
$email = 'myemail';
$password = 'mypassword';
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$tokenurl = 'https://squareup.com/login/';
curl_setopt($ch, CURLOPT_URL, $tokenurl);
$content = curl_exec($ch);
$get_auth_token = strstr($content, 'input name="authenticity_token" type="hidden" value="');
$auth_token = substr($get_auth_token,53,44);
$loginUrl = 'https://www.squareup.com/login/';
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'email='.$email.'&password='.$password.'&authenticity_token='.$auth_token);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
$store = curl_exec($ch);
$fileurl = 'https://squareup.com/dashboard';
curl_setopt($ch, CURLOPT_URL, $fileurl);
$content = curl_exec($ch);
file_put_contents('data.txt', $content);
The result in my data.txt file is either the login page being displayed again (as in, it didn't login correctly).
Any tips appreciated!

curl fetching content of redirected webpage

I try to login in a site which has a combined login.
The redirect to this site works fine and the redirect back as well ... but only if I stop the PHP programm with die(). Unfortunately it is not stored in $return, which I would need to proceed.
Any ideas, what I am doing wrong? Much appreciated!
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, TRUE);
curl_setopt($ch, CURLOPT_POSTREDIR, TRUE);
curl_setopt($ch, CURLOPT_URL, URI_LOGIN);
$content = curl_exec($ch);
// PREPARING THE LOGIN
$referer = URI_LOGIN . '?ReturnUrl=' . urlencode('/returnUrl/');
curl_setopt($ch, CURLOPT_URL, $referer);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE);
$post = ...;
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post) );
curl_setopt($ch, CURLOPT_USERAGENT, '...');
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
$content = curl_exec($ch);
// LOGIN
curl_setopt($ch, CURLOPT_URL, COMBINED_LOGIN);
$post = ...;
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post) );
$return = curl_exec($ch);
// ...
?>
There was a problem with javascript. It stucked on an 'in-between'-page and I had to make another curl request to get to the right page.
Now it is working fine!

Categories