php 模拟http,PHP-Curl模拟HTTP请求

使用PHP-Curl方式模拟HTTP请求,测试接口传参和返回值状态

/**

* 模拟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请求是等效的,如下:

6e43f5359c759ea2782564479a8e7ada.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值