使用PHP-Curl方式模拟HTTP请求,测试接口传参和返回值状态
<?php
/**
* 模拟post进行url请求
* @param string $url
* @param array $postData
*/
function request_post($url = '', $postData = array()) {
if (empty($url) || empty($postData)) {
return false;
}
$postUrl = $url;
$ch = curl_init();//初始化curl
//转义
$vars = http_build_query($postData, '', '&');
curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);//参数
$data = curl_exec($ch);//运行curl
curl_close($ch);
return $data;
}
/**
* 测试
* @param string $url
*/
function testAction() {
$url = 'http://www.testing2.ifchange.com/atsng/atsInternal/insertRecruitMessage';
$postData['app_id'] = 5;
$postData['uid'] = 1226;
$postData['user_id'] = 1226;
$postData['tob_resume_id'] = 0;
$postData['tob_position_id'] = 0;
$postData['type'] = 14;
$postData['content'] = array('');
$res = request_post($url, $postData);
print_r($res);
}
testAction();
结果:
{"err_msg":"","err_no":0,"results":{"uid":"1226","type":"14","tob_position_id":"0","tob_resume_id":"0","content":"
[null]","status":0,"updated_at":"2018-05-03 15:46:32","created_at":"2018-05-03 15:46:32","message_id":3306}}
这个请求和Postman请求是等效的,如下: