在php扩展库curl中可以通过函数“curl_setopt()”设置选项常量“curlopt_httpheader”,来设置header,使用时将header数据传入第3个参数即可。
<?php$header = array( 'x-api-key:'.'b8602c0361111415a221759cdeb9e636', 'content-type:'.'application/x-www-form-urlencoded; charset=utf-8');/** * @param $url * @param null $data * @return bool|string */public function http_request($url, $data = null, $header = null){ $curl = curl_init(); if(!empty($header)){ curl_setopt($curl, curlopt_httpheader, $header); curl_setopt($curl, curlopt_header, 0);//返回response头部信息 } curl_setopt($curl, curlopt_url, $url); curl_setopt($curl, curlopt_ssl_verifypeer, false); curl_setopt($curl, curlopt_ssl_verifyhost, false); if (!empty($data)) { curl_setopt($curl, curlopt_httpget, 1); curl_setopt($curl, curlopt_postfields,http_build_query($data)); } curl_setopt($curl, curlopt_returntransfer, true); $output = curl_exec($curl); curl_close($curl); return $output;}
推荐教程:《php》
以上就是php中curl如何设置header?的详细内容。
