我试图通过API来管理Jenkins的工作。通过命令行,使用此行访问数据没有问题:
curl -v --noproxy localhost, http://localhost:4545/job/Test/api/json --user username:token
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;
}
我也尝试设置代理,但我还是得到一个代码错误403警告。