The editor below will share with you a method of sending custom data through header in PHP. It has a good reference and value for learning PHP. I hope it will be helpful to everyone. If you are interested in PHP, please follow the editor to take a look.
This article will introduce how to send custom data through headers. When sending a request, in addition to using $_GET/$_POST to send data, you can also put the data in the header and transmit it.
Send header:
We have defined three parameters, token, language, region, put header and send it over
<?php $url = 'http://www.example.com'; $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 '
'; 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; } ?>
Receive header
We can $ Obtain header data from _SERVER. Custom data uses HTTP_ as the prefix, so the data with the HTTP_ prefix can be read out.
<?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); /** * 获取自定义的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; } ?>
Output:
POST data: Array ( [name] => fdipzone ) Header data: Array ( [token] => JxRaZezavm3HXM3d9pWnYiqqQC1SJbsU [language] => zh [region] => GZ )
The above method of sending custom data through header in php is all the content shared by the editor. I hope it can give you a reference. ! !
Related recommendations:
PHP implementation that can add watermarks and generate thumbnail processing tools
Detailed explanation of PHP serialization and deserialization principles
The above is the detailed content of PHP sends custom data method through header. For more information, please follow other related articles on the PHP Chinese website!

Win11如何自定义背景图片?在最新发布的win11系统中,里面有许多的自定义功能,但是很多小伙伴不知道应该如何使用这些功能。就有小伙伴觉得背景图片比较单调,想要自定义背景图,但是不知道如何操作自定义背景图,如果你不知道如何定义背景图片,小编下面整理了Win11自定义背景图片步骤,感兴趣的话一起往下看看把!Win11自定义背景图片步骤1、点击桌面win按钮,在弹出的菜单中点击设置,如图所示。2、进入设置菜单,点击个性化,如图所示。3、进入个性化,点击背景,如图所示。4、进入背景设置,点击浏览图片

维恩图是用来表示集合之间关系的图。要创建维恩图,我们将使用matplotlib。Matplotlib是一个在Python中常用的数据可视化库,用于创建交互式的图表和图形。它也用于制作交互式的图像和图表。Matplotlib提供了许多函数来自定义图表和图形。在本教程中,我们将举例说明三个示例来自定义Venn图。Example的中文翻译为:示例这是一个创建两个维恩图交集的简单示例;首先,我们导入了必要的库并导入了venns。然后我们将数据集创建为Python集,之后,我们使用“venn2()”函数创

CakePHP是一个强大的PHP框架,为开发人员提供了很多有用的工具和功能。其中之一是分页,它可以帮助我们将大量数据分成几页,从而简化浏览和操作。默认情况下,CakePHP提供了一些基本的分页方法,但有时你可能需要创建一些自定义的分页方法。这篇文章将向您展示如何在CakePHP中创建自定义分页。步骤1:创建自定义分页类首先,我们需要创建一个自定义分页类。这个

Vue是一款流行的JavaScript框架,它提供了许多方便的功能和API以帮助开发者构建交互式的前端应用程序。随着Vue3的发布,render函数成为了一个重要的更新。本文将介绍Vue3中render函数的概念、用途和如何使用它自定义渲染函数。什么是render函数在Vue中,template是最常用的渲染方式,但是在Vue3中,可以使用另外一种方式:r

linux的header是指在文件或数据流中的开头部分,用于包含关于内容的元数据,通过正确地编写和使用Header文件,开发者能够更好地利用系统资源,提高代码的可读性和可维护性。

适用于iPhone的iOS17更新为AppleMusic带来了一些重大变化。这包括在播放列表中与其他用户协作,在使用CarPlay时从不同设备启动音乐播放等。这些新功能之一是能够在AppleMusic中使用交叉淡入淡出。这将允许您在曲目之间无缝过渡,这在收听多个曲目时是一个很棒的功能。交叉淡入淡出有助于改善整体聆听体验,确保您在音轨更改时不会受到惊吓或退出体验。因此,如果您想充分利用这项新功能,以下是在iPhone上使用它的方法。如何為AppleMusic啟用和自定Crossfade您需要最新的

【SpringBoot】通过Feign调用传递Header中参数如何通过Feign传递Header参数问题描述我们在SpringCloud中使用Feign请求另一个服务的Api接口时,有将Header中参数传递下去的需求,如果不做特殊处理,就会将Header中的参数丢失。解决方案方案一:通过@RequestHeader(name="headerName")来传递例如:Feign定义如下@FeignClient(name="service-name")pub

如何在CodeIgniter中实现自定义中间件引言:在现代的Web开发中,中间件在应用程序中起着至关重要的作用。它们可以用来执行在请求到达控制器之前或之后执行一些共享的处理逻辑。CodeIgniter作为一个流行的PHP框架,也支持中间件的使用。本文将介绍如何在CodeIgniter中实现自定义中间件,并提供一个简单的代码示例。中间件概述:中间件是一种在请求


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
