php 定義header參數和接收


定義---

ajax方式:

 

$.ajax({  

                   type: "GET",  

                   url: "default.aspx",  

                    beforeSend: function(request) {  

                        request.setRequestHeader("Test", "Chenxizhang");  

                    },  

                    success: function(result) {  

                        alert(result);  

                    }  

                }) 

 

curl方式:

$url = 'http://www.example.com'; $header = array('token:JxRaZezavm3HXM3d9pWnYiqqQC1SJbsU','language:zh','region:GZ'); $content = array( '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']  
//如上所示,在PHP中所有的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;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM