php定义header参数和接收定义---
ajax⽅式:
$.ajax({
type: "GET",
url: "default.aspx",
beforeSend: function(request) {
request.setRequestHeader("Test", "Chenxizhang");
},
success: function(result) {
alert(result);
}
})
curl⽅式:
$url = 'ample';
$header = array('token:JxRaZezavm3HXM3d9pWnYiqqQC1SJbsU','language:zh','region:GZ');
$content = array(php中header是什么意思
'name' => 'fdipzone'
);
$response = curlSend($url, $header,'', $content);
public function curlSend($url, $header, $file = '',$param, $post = true) {
$streamContent = "";
if ($file) {
$handle = fopen($file, 'rb');
do {
$dataUnit = fread($handle, 8192);
if (strlen($dataUnit) == 0) {
break;
}
$streamContent .= $dataUnit;
} while (true);
fclose($handle);
}
$ch = curl_init();
if(!empty($header)){
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
//设置请求⽅式
if ($post) {
curl_setopt($ch, CURLOPT_POST, TRUE);
if(streamContent){
curl_setopt($ch, CURLOPT_POSTFIELDS, $streamContent);
}
if(param){
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
}
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_NOBODY, FALSE);
} else {
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
}
$data = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return array('httpCode' => $httpCode, 'response' => json_decode($data, true));
}
获取:
直接获取
$_SERVER['HTTP_TEST']
//如上所⽰,在中所有的header中的⾃定义信息都会被加上HTTP_的开头,在获取的时候参数名称⽆论⼤⼩写全部转换成⼤写!/** * 获取⾃定义的header数据 */
function get_all_headers(){
// 忽略获取的header数据
$ignore = array('host','accept','content-length','content-type');
$headers = array();
foreach($_SERVER as $key=>$value){
if(substr($key, 0, 5)==='HTTP_'){
$key = substr($key, 5);
$key = str_replace('_', ' ', $key);
$key = str_replace(' ', '-', $key);
$key = strtolower($key);
if(!in_array($key, $ignore)){
$headers[$key] = $value;
}
}
}
return $headers;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。