Jenkins API-连接被拒绝或错误403

时间:2019-03-14 11:17:02

标签: php jenkins

我正在尝试通过API管理詹金斯的工作。通过命令行,使用此行访问数据没有问题:

curl -v --noproxy localhost, http://localhost:4545/job/Test/api/json --user username:token

(Jenkins正在localhost:4545上运行)。 --noproxy部分很重要,否则我有一个403 error

当我尝试在PHP文件中执行相同操作时,它不起作用。

这是我的API代码:

function getLastBuildStatus($url, $jobName, $username, $api_token){
    $ch = initCurl($username, $api_token); //see function downside

    curl_setopt($ch, CURLOPT_URL, $url . '/job/' . $jobName . '/lastBuild/api/json');

    curl_setopt($ch, CURLOPT_NOPROXY, "localhost"); //return Error:Failed to connect to localhost port 4545: Connection refused
    curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, true); //return Error 403 Access denied

    $result = executeCurl($ch);
    $json = json_decode($result);
    var_dump ($result);
    if($json){
        return $json->result;
    }
    else {
        return -1;
    }

function initCurl($username, $api_token){
    $ch = curl_init();
    curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false ); 
    curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, false ); 
    curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $api_token); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    return $ch;
}

我也尝试设置代理,但是我也得到了error 403 forbidden

0 个答案:

没有答案