이 글은 주로 PHP에서 헤더를 통해 사용자 정의 데이터를 전송하는 방법을 공유합니다. 이는 좋은 참고 가치가 있으며 모든 사람에게 도움이 되기를 바랍니다. 편집자를 따라 살펴보겠습니다. 모두에게 도움이 되기를 바랍니다.
헤더 보내기:
토큰, 언어, 지역이라는 세 가지 매개변수를 정의하고 이를 헤더에 넣은 다음
1 2 3 4 5 6 7 8 9 10 11 12 | <?php
$url = 'http:
$header = array ('token:JxRaZezavm3HXM3d9pWnYiqqQC1SJbsU','language:zh','region:GZ');
$content = array (
'name' => 'fdipzone'
);
$response = tocurl( $url , $header , $content );
$data = json_decode( $response , true);
echo 'POST data:' ;
echo '<pre class="brush:php;toolbar:false">' ;
print_r( $data [ 'post' ]);
echo '
|
';
echo 'Header data:';
echo '
1 2 3 | ';
print_r( $data [ 'header' ]);
echo '
|
';
/**
* 发送数据
* @param String $url 请求的地址
* @param Array $header 自定义的header数据
* @param Array $content POST的数据
* @return String
*/
function tocurl($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;
}
?>
로그인 후 복사
헤더 수신
을 통해 보냅니다. $_SERVER에서 헤더 데이터를 얻을 수 있으며 사용자 정의된 데이터는 다음을 사용합니다. HTTP_가 접두어로 사용되므로 HTTP_ 접두사가 있는 데이터를 읽을 수 있습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <?php
$post_data = $_POST ;
$header = get_all_headers();
$ret = array ();
$ret ['post'] = $post_data ;
$ret ['header'] = $header ;
header('content-type:application/json;charset=utf8');
echo json_encode( $ret , JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
function get_all_headers(){
$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 ;
}
?>
|
로그인 후 복사
출력:
1 2 3 4 5 6 7 8 9 10 11 12 | POST data:
Array
(
[name] => fdipzone
)
Header data:
Array
(
[token] => JxRaZezavm3HXM3d9pWnYiqqQC1SJbsU
[language] => zh
[region] => GZ
)
|
로그인 후 복사
관련 권장 사항:
사용자 정의 데이터 속성에 대한 권장 10개 기사
HTML5 실제 전투 및 사용자 정의 데이터 속성 분석(데이터 세트)
php는 헤더 정의를 통해 전송됩니다.
위 내용은 헤더를 통해 사용자 정의 데이터를 보내는 PHP에 대한 자세한 설명의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!