希望踩过的坑能为您铺平前进的路
UPDN .CN

php通过jenkins api获取信息

先上代码

<?php
class get_jenkins_api_info{



function get_jenkins_author ($jenkins_url, $jenkins_user, $jenkins_password, $jenkins_job_name){
    //CURL 账号密码 添加授权Authorization头Header
    $arr_header[] = "Content-Type:application/json";
    $arr_header[] = "Authorization: Basic ".base64_encode("$jenkins_user:$jenkins_password");
    //jenkins api 接口地址
    $jenkins_api = "$jenkins_url/jenkins/job/$jenkins_job_name/description/api";
    //--请求开始--
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $jenkins_api);
    //设置头文件的信息作为数据流不输出
    curl_setopt($ch, CURLOPT_HEADER, 0);
    //设置获取的信息以文件流的形式返回,而不是直接输出。
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $arr_header);
    $result = curl_exec($ch);
    curl_close($ch);
    //--请求关闭--
    //--请求结果正则处理--
    $pieces_1 = explode("author:", $result);
    $pieces = $pieces_1[1];
    $pieces_2 = explode("\r\n",$pieces);
    $pieces = $pieces_2[0];
    //按照英文,去间隔多个author
    $pieces_3 = explode(",",$pieces);
    //--请求结果正则处理完毕--
    //返回author数组
    return $pieces_3;
}


function get_jenkins_module ($jenkins_url, $jenkins_user ,$jenkins_password, $jenkins_job_name){
    //CURL 账号密码 添加授权Authorization头Header
    $arr_header[] = "Content-Type:application/json";
    $arr_header[] = "Authorization: Basic ".base64_encode("$jenkins_user:$jenkins_password");
    //jenkins api 接口地址
    $jenkins_api = "$jenkins_url/jenkins/job/$jenkins_job_name/description/api";
    //--请求开始--
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $jenkins_api);
    //设置头文件的信息作为数据流不输出
    curl_setopt($ch, CURLOPT_HEADER, 0);
    //设置获取的信息以文件流的形式返回,而不是直接输出。
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $arr_header);
    $result = curl_exec($ch);
    curl_close($ch);
    //--请求关闭--
    //--请求结果正则处理--
    $pieces_1 = explode("module:", $result);
    $pieces = $pieces_1[1];
    $pieces_2 = explode("\r\n",$pieces);
    $pieces = $pieces_2[0];
    //按照英文,去间隔多个author
    $pieces_3 = explode(",",$pieces);
    //--请求结果正则处理完毕--
    //返回author数组
    return $pieces_3;
}


function get_jenkins_module ($jenkins_url, $jenkins_user ,$jenkins_password, $jenkins_job_name){
    //CURL 账号密码 添加授权Authorization头Header
    $arr_header[] = "Content-Type:application/json";
    $arr_header[] = "Authorization: Basic ".base64_encode("$jenkins_user:$jenkins_password");
    //jenkins api 接口地址
    $jenkins_api = "$jenkins_url/jenkins/job/$jenkins_job_name/description/api";
    //--请求开始--
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $jenkins_api);
    //设置头文件的信息作为数据流不输出
    curl_setopt($ch, CURLOPT_HEADER, 0);
    //设置获取的信息以文件流的形式返回,而不是直接输出。
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $arr_header);
    $result = curl_exec($ch);
    curl_close($ch);
    //--请求关闭--
    //--请求结果正则处理--
    $pieces_1 = explode("module:", $result);
    $pieces = $pieces_1[1];
    $pieces_2 = explode("\r\n",$pieces);
    $pieces = $pieces_2[0];
    //按照英文,去间隔多个author
    $pieces_3 = explode(",",$pieces);
    //--请求结果正则处理完毕--
    //返回author数组
    return $pieces_3;
}

}


// 调用方式
$jenkins_api= new get_jenkins_api_info();
$jenkins_api_author_arry=$jenkins_api->get_jenkins_author("http://jenkins.ke.com","gaoyaohua001","123456","iprd-crm-online-build");
$jenkins_api_module_arry=$jenkins_api->get_jenkins_module("http://jenkins.ke.com","gaoyaohua001","123456","iprd-crm-online-build");
var_dump($jenkins_api_author_arry);
var_dump($jenkins_api_module_arry);
?>

核心是jenkins提供了api
通过get方式,加上认证的head头,请求到json然后进行处理

文章转载请标注:http://www.updn.cnUPDN博客 » php通过jenkins api获取信息

评论 抢沙发

UPDN

关于我们联系我们