java写post 服务端_curl post请求java服务端提供的接口

post请求此接口是通的,但是PHP程序中请求就报500 ,下面贴代码 参数是他们需要的参数,格式也是按照定下来的标准。。很诡异。。各位大神遇到过吗

$this->sendRequest('POST', $url, json_encode($params), ['Content-Type: application/json']);

/**

* 发送HTTP请求,获取响应结果

*

* @param string $method 请求方法

* @param string $url 请求地址

* @param array $params 请求体参数

* @param array $headers 请求头

* @param bool|string $https 是否启用证书认证,可传入证书路径

*

* @return array

*/

protected function sendRequest($method, $url, $params = [], $headers = [], $https = false)

{

// 标准化请求方法

$method = strtoupper($method);

$ch = curl_init();

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($ch, CURLOPT_URL, $url);

// 写入Headers

if ($headers) {

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

}

// 控制请求参数

if ($method != 'GET') {

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

if ($method != 'POST') {

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);

}

}

// Https证书

if (stripos($url, 'https') === 0) {

if ($https && is_string($https)) { // 传入证书路径

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);

curl_setopt($ch, CURLOPT_CAINFO , $https);

} else {

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $https ? 2 : 0);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $https ? 1 : 0);

}

}

curl_exec($ch);

$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

$content = curl_multi_getcontent($ch);

curl_close($ch);

return [

'status' => $status,

'content' => $content

];

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值