liqinggai

定义---

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;
}

分类:

技术点:

相关文章:

  • 2022-02-04
  • 2021-05-20
  • 2021-08-20
  • 2021-04-02
  • 2021-05-30
  • 2021-09-08
  • 2021-11-08
猜你喜欢
  • 2021-07-07
  • 2021-11-28
  • 2021-06-27
  • 2021-11-28
  • 2021-06-13
  • 2021-11-02
  • 2021-11-02
相关资源
相似解决方案