1

所以,我一直在尝试使用我构建的脚本执行函数 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);
?>
4

1 回答 1

0

尝试一些调试,想error_log($cpcmd_url);看看url是否正确。另外,在CURLOPT_POSTFIELDS, cpanel? 对我来说似乎错了。

于 2013-06-05T18:33:00.360 回答