php测试api接口的软件,PHP测试https API接口的方法

以前也没有接触过通过https来测试API接口的方法,今天工作中用到了,也实现了。总结一下:大致分以下几个部分:

1、增加 证书:利用PHP将证书加载到脚本中如下:

//加载证书的函数

function sslInit($ch){

//证书-start

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);

curl_setopt($ch, CURLOPT_VERBOSE, 1);

curl_setopt($ch, CURLOPT_CAINFO, './cert/ca_cert.pem');//根证书

curl_setopt($ch, CURLOPT_SSLCERTTYPE,'PEM');

curl_setopt($ch, CURLOPT_SSLCERT, './cert/client_cert.pem');//公钥

curl_setopt($ch, CURLOPT_SSLKEYTYPE,'PEM');//私钥文件格式

curl_setopt($ch, CURLOPT_SSLKEY,'./cert/client_key.pem');//私钥

curl_setopt($ch, CURLOPT_SSLKEYPASSWD,'111111');//私钥的密码

//证书-end

curl_setopt($ch, CURLOPT_COOKIEJAR,'./logs/cookie.txt'); //保存

curl_setopt($ch, CURLOPT_COOKIEFILE,'./logs/cookie.txt');//读取

return $ch ;

}

2、实现登录 通过加载证书来验证用户的合法性及签名是否正确

function login(){

//fengbo.xue@quanshi.com q111111

$xml_data = '<?xml version="1.0" encoding="utf-8"?>';

$xml_data .= '';

$xml_data .= 'XXXXXX@XX.COM';

$xml_data .= 'tester';

$xml_data .= '';

$url = 'https://XXX.XX.XX/login';

$header[] = "Content-type: text/xml";//定义content-type为xml

$ch = curl_init();

$ch = $this->sslInit($ch);

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);

$response = curl_exec($ch);

//echo $response;

return $response ;

if(curl_errno($ch)) {

print_r(curl_error($ch));

}

curl_close($ch);

}

3、加载要测试的API

如:

function join($LshyID)

{

$xml_data = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

2

1

';

$url = 'https://xxx.xxx.xxx/join/SME-MEETING/'.$LshyID;

$header[] = "Content-type: application/xml";//定义content-type为xml

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);

//证书-start

$ch=$this->sslInit($ch);

$response = curl_exec($ch);

return $response;

if(curl_errno($ch)) {

print_r(curl_error($ch));

}

curl_close($ch);

}

总结:又是一个三步曲。。。哈哈。。 自己发现写代码还挺好玩的。以前还真想走开发的路。

希望对正迷茫的朋友,正在查找这方面的测试资源的朋友一点指导。哈哈。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值