PHP发送HTTP请求查询JIRA
以前做QA平台时用python写过一个函数,通过REST API查询JIRA系统里的ticket记录,采用的是Python的requests包里提供的post方法。
这阵子在考虑用PHP做后端取代Python django的后端(顺便学习一下PHP建站基础),在处理表单、登录系统和数据库接口之外,还得实现和JIRA系统的查询接口,今天从网上学习了一些PHP发出HTTP请求的代码,整理出分别用GET和POST两个方法获取JIRA ticket信息。
发送HTTP请求的基本方法
PHP发送HTTP请求最基本包括下面几条函数调用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php // 创建一个新cURL资源 $ch = curl_init(); // 设置URL和相应的选项 curl_setopt($ch, CURLOPT_URL, "http://www.example.com/"); curl_setopt($ch, CURLOPT_HEADER, 0); // 抓取URL并把它传递给浏览器 curl_exec($ch); // 关闭cURL资源,并且释放系统资源 curl_close($ch); ?> |
几个要点
如果系统要求登录认证,则需要加入下面一行。
1 2 3 4 5 |
<?php // $user表示用户名 // $password表示密码 curl_setopt($ch, CURLOPT_USERPWD, $user:$password); ?> |
除此之外还可以通过定义HTTP头部的Authorization来传输base64的认证信息,相对于代码中明文的用户名和密码,base64形式也是一种对密码的基本保护。
1 2 3 4 |
<?php // Z7FuaLFpqm1EYmk1Mnlv为经过base64编码后的$user:$password curl_setopt($ch,CURLOPT_HTTPHEADER, array('Authorization: Basic Z7FuaLFpqm1EYmk1Mnlv')); ?> |
以上的基本代码针对的是http协议的网站,对于https协议的JIRA网站,还需加入下面的设定。
1 2 3 4 |
<?php curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0); // 跳过证书检查 curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0); // 跳过检查服务器SSL证书的域名 ?> |
完整示例
JIRA系统的REST API的链接是https://jira.example.com/rest/api/2/search, 查询tickets返回的json结构如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
{ "maxResults": 2, "startAt": 0, "total": 18, "expand": "schema,names", "issues": [ { "expand": "html", "id": "10393", "key": "QA-36", "self": "http://kelpie9:8081/rest/api/2/issue/QA-36", "transitions": "http://kelpie9:8081/rest/api/2/issue/QA-36/transitions" }, { "expand": "html", "id": "10389", "key": "QA-35", "self": "http://kelpie9:8081/rest/api/2/issue/QA-35", "transitions": "http://kelpie9:8081/rest/api/2/issue/QA-35/transitions" } ] } |
下面分别给出通过GET和POST方法获取指定ticket的summary的完整代码。
GET方法查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php $ch=curl_init(); curl_setopt($ch,CURLOPT_URL,'https://jira.example.com/rest/api/2/search/?jql=key=VIS-740&fields=key,summary'); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_HTTPHEADER, array('Content-Type: application/json','Authorization: Basic Z7FuaLFpqm1EYmk1Mnlv')); //设置登录用户名密码和content-type curl_setopt($ch,CURLOPT_HEADER,0); // 不返回response headers curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0); // 跳过证书检查 curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0); // 跳过检查服务器SSL证书的域名 $raw_output = curl_exec($ch); curl_close($ch); $output = json_decode($raw_output); foreach ($output->issues as $issue) { echo $issue->key . ' ' . $issue->fields->summary . '<br />'; } ?> |
POST方法查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php $fields = array('key', 'summary'); $data = json_encode(array('jql'=>'key in (VIS-740, VIS-741)', 'fields'=>$fields)); $ch=curl_init(); curl_setopt($ch,CURLOPT_URL,'https://jira.example.com/rest/api/2/search'); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_HTTPHEADER, array('Content-Type: application/json','Authorization: Basic Z7FuaLFpqm1EYmk1Mnlv')); //设置登录用户名密码和content-type curl_setopt($ch,CURLOPT_HEADER,0); // 不返回response headers curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0); // 跳过证书检查 curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0); // 跳过检查服务器SSL证书的域名 curl_setopt($ch,CURLOPT_POST,1); //设置是通过post还是get方法 curl_setopt($ch,CURLOPT_POSTFIELDS,$data); //传递POST的变量 $raw_output = curl_exec($ch); curl_close($ch); $output = json_decode($raw_output); foreach ($output->issues as $issue) { echo $issue->key . ' ' . $issue->fields->summary . '<br />'; } ?> |
等效curl命令
1 2 3 4 5 6 |
# GET版本 curl -D- \ -u user:password \ -X GET \ -H "Content-Type: application/json" \ "https://jira.example.com/rest/api/2/search/?jql=key in (VIS-740, VIS-741)&fields=key,summary" |
1 2 3 4 5 6 7 |
# POST版本 curl -D- \ -u user:password \ -X POST \ -H "Content-Type: application/json" \ --data '{"jql":"key in (VIS-740, VIS-741)","fields":["key","summary"]}' \ "https://jira.example.com/rest/api/2/search" |
本文出自扉启博客,转载时请注明出处及相应链接。
本文永久链接: https://www.feiqy.com/php-http-request-jira/
近期评论