php如何使用curl设置header头传参

发布时间:2023-09-23 点击:126
php curl设置header的方法:首先初始化浏览器;然后设置浏览器,并把参数url传到浏览器的设置当中;接着以字符串形式返回到浏览器当中,并判断data是否有数据;最后通过“curl_setopt”方法设置header头即可。
推荐:《php教程》
php使用curl设置header头传参以及设置content-type: application/json类型的后台数据接收
curl函数
public function curlrequest($url,$data=null,$header=null){//初始化浏览器$ch = curl_init();//设置浏览器,把参数url传到浏览器的设置当中curl_setopt($ch, curlopt_url, $url);//以字符串形式返回到浏览器当中curl_setopt($ch, curlopt_returntransfer, true);//禁止https协议验证域名,0就是禁止验证域名且兼容php5.6curl_setopt($ch, curlopt_ssl_verifyhost, 0);//禁止https协议验证ssl安全认证证书curl_setopt($ch, curlopt_ssl_verifypeer, false);//判断data是否有数据,如果有data数据传入那么就把curl的请求方式设置为post请求方式if ( !empty($data) ) {//设置post请求方式@curl_setopt($ch, curlopt_post, true);//设置post的数据包@curl_setopt($ch, curlopt_postfields, $data);}//设置header头if ( !empty($header) ) {curl_setopt($ch, curlopt_httpheader, $header);}//让curl发起请求$str = curl_exec($ch);//关闭curl浏览器curl_close($ch);//把请求回来的数据返回return $str;}函数调用
$url = "http://desk.yunindex.cn/test";$jsonarr = ['accountid' => '34','option' => 'chat_index',];$jsonstr = json_encode($jsonarr);//设置了content-type: application/json,传参要转化为json,否则后台接收不到数据//header头设置$headers = array("content-type: application/json","content-length: " . strlen($jsonstr) . "","accept: application/json","client-id:5k8264iltkch16cq2502si8znmtm67vs",//自定义参数"client-secret:c380bec2bfd727a4b6845133519f3ad6"//自定义参数);$data = $this->curlrequest($url,$jsonstr,$headers);后台接受
json_encode($_server)使用$_server能接收到header头的信息,在header头里面设置的自定义参数会在这里面体现
结果
{"user": "www","home": "\\\\/home\\\\/www","http_client_secret": "c380bec2bfd727a4b6845133519f3ad6", //这是自定义的参数,跟上面传的参数相对于,会自动转化为大写跟下划线"http_client_id": "5k8264iltkch16cq2502si8znmtm67vs",  //这是自定义的参数,跟上面传的参数相对于,会自动转化为大写跟下划线"http_accept": "application\\\\/json","http_content_length": "40","http_content_type": "application\\\\/json","http_host": "desk.yunindex.cn","script_filename": "\\\\/workspace\\\\/php\\\\/index_desk\\\\/htdocs\\\\/index.php","redirect_status": "200","server_name": "desk.yunindex.cn","server_port": "80","server_addr": "192.168.1.90","remote_port": "64524","remote_addr": "192.168.1.113","server_software": "nginx\\\\/1.15.2","gateway_interface": "cgi\\\\/1.1","request_scheme": "http","server_protocol": "http\\\\/1.1","document_root": "\\\\/workspace\\\\/php\\\\/index_desk\\\\/htdocs","document_uri": "\\\\/index.php","request_uri": "\\\\/test","script_name": "\\\\/index.php","content_length": "40","content_type": "application\\\\/json","request_method": "post","query_string": "","fcgi_role": "responder","php_self": "\\\\/index.php","request_time_float": 1557025085.410574,"request_time": 1557025085}这里有一个问题,如果使用的是nginx的话,header头里的参数如果有下划线(如:client_secret),使用$_server貌似会接收不到该参数,需要配置nginx.conf,在里面添加 underscores_in_headers on; 配置
file_get_contents('php://input')接收参数,以流的形式接收
结果
string(40) "{"accountid":"34","option":"chat_index"}"

关于建站手机的几点建议
基于spring boot java 邮箱功能
购买域名的网站和买域名的技巧
注册一个org域名多少钱
云服务器ecs使用教程帮助文档
网站安全防护 该如何加固网站的session安全
云服务器怎么搭建ftp,有哪些具体步骤?
网站正在建设中,具备哪些设计要点?