php post 设置header json传参

public function urlGetContent(url,params = null, method = 'GET',json = false)
{
ch = curl_init(); curl_setopt(ch, CURLOPT_URL, url); curl_setopt(ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt(ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt(ch, CURLOPT_HEADER, 0);
if (method == 'POST') { curl_setopt(ch, CURLOPT_POST, true);
if (json && is_array(params)) {
params = json_encode(params);
}
if (is_string(params) || is_array(params)) {
curl_setopt(ch, CURLOPT_POSTFIELDS,params);
} else {
// error_log("Argument $params should be an array of parameters or (if you want to send raw data) a string");
return false;
}
if (json) { //发送JSON数据 curl_setopt(ch, CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json; charset=utf-8',
'Content-Length:' . strlen(params), } } curl_setopt(ch, CURLOPT_RETURNTRANSFER, 1);
output = curl_exec(ch);
curl_close(ch); returnoutput;
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容