如何在PHP中设置自定义REQUEST标头 [英] How to set custom REQUEST header in PHP

查看:89
本文介绍了如何在PHP中设置自定义REQUEST标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建RESPONSE标头时,使用header()函数可以解决问题.请求标头怎么样?

In creating RESPONSE headers, using header() function will do the trick. How about for request headers?

提前谢谢!

推荐答案

如果您正在谈论发出请求并指定标头,则使用

If you are talking about making a request and specifying your headers, one way to do it is using CURL:

$data = "<soap:Envelope>[...]</soap:Envelope>"; 
$tuCurl = curl_init();
curl_setopt($tuCurl, CURLOPT_HEADER, 0);
curl_setopt($tuCurl, CURLOPT_URL, "http://example.com/path/for/soap/url/");
curl_setopt($tuCurl, CURLOPT_POST, 1); // if post request
curl_setopt($tuCurl, CURLOPT_POSTFIELDS, $data);
curl_setopt($tuCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($tuCurl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml","SOAPAction: \"/soap/action/query\"", "Content-length: ".strlen($data)));
$tuData = curl_exec($tuCurl); 
if(!curl_errno($tuCurl)){ 
  $info = curl_getinfo($tuCurl); 
  echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url']; 
} else { 
  echo 'Curl error: ' . curl_error($tuCurl); 
} 
curl_close($tuCurl); 
echo $tuData; 

这篇关于如何在PHP中设置自定义REQUEST标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆