php设置自定义header 参数
/**
* curl 请求发送
* @param $url
* @param $header
* @param $content
* @return mixed
*/
function send($url, $header, $content){
$ch = curl_init();
if(substr($url,0,5)=='https'){
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true); // 从证书中检查SSL加密算法是否存在
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($content));
$response = curl_exec($ch);
if($error=curl_error($ch)){
die($error);
}
curl_close($ch);
return $response;
}
$url = 'http://www.example.com';
$header = array('token:JxRaZezavm3HXM3d9pWnYiqqQC1SJbsU','language:zh','region:GZ');
$content = array(
'name' => 'fdipzone'
);
$response = send($url, $header, $content);
$data = json_decode($response, true);
echo 'POST data:';
echo '
';
print_r($data['post']);
echo '
';echo 'Header data:';
echo '
';
print_r($data['header']);
echo '
';php获取header 参数【自定义】
/**
* 获取所有 以 HTTP开头的header参数
* @return array
*/
function getAllHeaders(){
$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);
$headers[$key] = $value;
}
}
return $headers;
}
$post_data = $_POST; //获取post参数作为对比
$header = getAllHeaders();
$ret = array();
$ret['post'] = $post_data;
$ret['header'] = $header;
//echo json_encode($_SERVER,JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
//注意:$_SERVER 可以获取所有 header的参数
//所有在header中自定义的参数 例如:自定义参数名:username 那么 获取方法 $_SERVER['HTTP_USERNAME'] 所有均是大写
//echo $_SERVER['HTTP_USERNAME'];
header('content-type:application/json;charset=utf8');
echo json_encode($ret, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
用户的相关信息可以放在header中 做网站 身份验证等操作欢迎大家,收藏转发哦!