如何使用cURL在cPanel中执行函数?

时间:2013-06-05 18:28:03

标签: php curl cron cpanel

所以,我一直在尝试使用我已构建的脚本执行函数Cron :: add_line,但它还没有正常工作。该脚本登录到cPanel,但在第二部分它没有执行json-api来执行add_line函数,我做了很多搜索,但从来没有找到任何东西让我理解它在cURL中是如何工作的。

如果我在浏览器中输入命令url,它会工作并创建cron,否则,使用我的脚本它不起作用。

如果有人可以帮助我欣赏!

谢谢!

代码:

<?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);
?>

1 个答案:

答案 0 :(得分:0)

尝试一些调试,例如error_log($cpcmd_url);以查看网址是否正确。另外,在CURLOPT_POSTFIELDS中, cpanel?对我来说似乎不对。