php如何向header头添加Authorization信息?

PHPz
Release: 2020-06-24 09:47:57
Original
4931 people have browsed it

php如何向header头添加Authorization信息?

php如何向header头添加Authorization信息?

php中,在header头添加Authorization信息需要在curl_exec()方法执行命令前,使用curl_setopt()方法添加Authorization信息。

具体介绍:

为了便于使用,这里定义一个get_data()函数来实现Authorization信息的传递,函数中有三个参数,第一个是发送请求的url,第二个是Authorization信息中的账号,第三个参数是Authorization信息中的密码。

1.png

Authorization信息使用curl来传输,首先要使用curl_init()方法初始化,再把Authorization信息保存在header头信息数组中。

2.png

头信息数组$headers包含两个元素,一个是编码信息,另一个就Authorization信息。Authorization信息包含账号和密码的信息。

3.png

使用curl_setopt()方法设置抓取的url,url由get_data()函数第一个参数提供 。

4.png

再次使用curl_setopt()方法设置头文件的信息作为数据流输出,并且设置获取的信息以文件流的形式返回,而不是直接输出。

5.png

使用curl_setopt()方法设置传输的header头信息,也就是已经设置了Authorization信息的数组$headers。使用curl_exec()方法执行命令,执行完命令,使用curl_close()方法关闭URL请求,至此,完成所有代码的编写。

在实际使用中,只需要调用该函数,并把请求的url,账号和密码(Authorization信息)作为参数传递给函数,即可成功实现认证信息的传递。

6.png

相关推荐:PHP视频教程

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!