http请求的时候Header加 Authorization值实现方式

2023-05-16

http请求的时候Header加 Authorization值实现方式

REST接口采用 HTTP基本认证的验证方式,HTTP Header(头)里加 Authorization

实现方法1

curl请求的时候 封装到headers

        $headers[]="Content-Type:application/json";
        $headers[]="Authorization: Basic ".$auth;
        ...
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        ...
实现方法2

curl请求的时候 CURLOPT_USERPWD:传递一个形如[username]:[password]风格的字符串,PHP去连接

        $authup=self::$app_key.":".self::$master_secret;
        ...
        curl_setopt($curl, CURLOPT_USERPWD, $authup);
        ...

curl异常捕获

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);// 参数为1表示输出信息头,为0表示不输出
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);// 参数为1返回,为0表示echo
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_USERPWD, $authup);// Authorization 认证方式
        $result = curl_exec($curl);
        
        $err = curl_error($curl);
        echo $err;
        
        curl_close($curl);

curl参数详解:

Usage: curl [options...] <url>
     --abstract-unix-socket <path> Connect via abstract Unix domain socket
     --anyauth       Pick any authentication method
 -a, --append        Append to target file when uploading
     --basic         Use HTTP Basic Authentication
     --cacert <CA certificate> CA certificate to verify peer against
     --capath <dir>  CA directory to verify peer against
 -E, --cert <certificate[:password]> Client certificate file and password
     --cert-status   Verify the status of the server certificate
     --cert-type <type> Certificate file type (DER/PEM/ENG)
     --ciphers <list of ciphers> SSL ciphers to use
     --compressed    Request compressed response
 -K, --config <file> Read config from a file
     --connect-timeout <seconds> Maximum time allowed for connection
     --connect-to <HOST1:PORT1:HOST2:PORT2> Connect to host
 -C, --continue-at <offset> Resumed transfer offset
 -b, --cookie <data> Send cookies from string/file
 -c, --cookie-jar <filename> Write cookies to <filename> after operation
     --create-dirs   Create necessary local directory hierarchy
     --crlf          Convert LF to CRLF in upload
     --crlfile <file> Get a CRL list in PEM format from the given file
 -d, --data <data>   HTTP POST data
     --data-ascii <data> HTTP POST ASCII data
     --data-binary <data> HTTP POST binary data
     --data-raw <data> HTTP POST data, '@' allowed
     --data-urlencode <data> HTTP POST data url encoded
     --delegation <LEVEL> GSS-API delegation permission
     --digest        Use HTTP Digest Authentication
 -q, --disable       Disable .curlrc
     --disable-eprt  Inhibit using EPRT or LPRT
     --disable-epsv  Inhibit using EPSV
     --dns-interface <interface> Interface to use for DNS requests
     --dns-ipv4-addr <address> IPv4 address to use for DNS requests
     --dns-ipv6-addr <address> IPv6 address to use for DNS requests
     --dns-servers <addresses> DNS server addrs to use
 -D, --dump-header <filename> Write the received headers to <filename>
     --egd-file <file> EGD socket path for random data
     --engine <name> Crypto engine to use
     --expect100-timeout <seconds> How long to wait for 100-continue
 -f, --fail          Fail silently (no output at all) on HTTP errors
     --fail-early    Fail on first transfer error, do not continue
     --false-start   Enable TLS False Start
 -F, --form <name=content> Specify HTTP multipart POST data
     --form-string <name=string> Specify HTTP multipart POST data
     --ftp-account <data> Account data string
     --ftp-alternative-to-user <command> String to replace USER [name]
     --ftp-create-dirs Create the remote dirs if not present
     --ftp-method <method> Control CWD usage
     --ftp-pasv      Use PASV/EPSV instead of PORT
 -P, --ftp-port <address> Use PORT instead of PASV
     --ftp-pret      Send PRET before PASV
     --ftp-skip-pasv-ip Skip the IP address for PASV
     --ftp-ssl-ccc   Send CCC after authenticating
     --ftp-ssl-ccc-mode <active/passive> Set CCC mode
     --ftp-ssl-control Require SSL/TLS for FTP login, clear for transfer
 -G, --get           Put the post data in the URL and use GET
 -g, --globoff       Disable URL sequences and ranges using {} and []
 -I, --head          Show document info only
 -H, --header <header/@file> Pass custom header(s) to server
 -h, --help          This help text
     --hostpubmd5 <md5> Acceptable MD5 hash of the host public key
 -0, --http1.0       Use HTTP 1.0
     --http1.1       Use HTTP 1.1
     --http2         Use HTTP 2
     --http2-prior-knowledge Use HTTP 2 without HTTP/1.1 Upgrade
     --ignore-content-length Ignore the size of the remote resource
 -i, --include       Include protocol response headers in the output
 -k, --insecure      Allow insecure server connections when using SSL 

     --interface <name> Use network INTERFACE (or address)
 -4, --ipv4          Resolve names to IPv4 addresses
 -6, --ipv6          Resolve names to IPv6 addresses
 -j, --junk-session-cookies Ignore session cookies read from file
     --keepalive-time <seconds> Interval time for keepalive probes
     --key <key>     Private key file name
     --key-type <type> Private key file type (DER/PEM/ENG)
     --krb <level>   Enable Kerberos with security <level>
     --libcurl <file> Dump libcurl equivalent code of this command line
     --limit-rate <speed> Limit transfer speed to RATE
 -l, --list-only     List only mode
     --local-port <num/range> Force use of RANGE for local port numbers
 -L, --location      Follow redirects
     --location-trusted Like --location, and send auth to other hosts
     --login-options <options> Server login options
     --mail-auth <address> Originator address of the original email
     --mail-from <address> Mail from this address
     --mail-rcpt <address> Mail from this address
 -M, --manual        Display the full manual
     --max-filesize <bytes> Maximum file size to download
     --max-redirs <num> Maximum number of redirects allowed
 -m, --max-time <time> Maximum time allowed for the transfer
     --metalink      Process given URLs as metalink XML file
     --negotiate     Use HTTP Negotiate (SPNEGO) authentication
 -n, --netrc         Must read .netrc for user name and password
     --netrc-file <filename> Specify FILE for netrc
     --netrc-optional Use either .netrc or URL
 -:, --next          Make next URL use its separate set of options
     --no-alpn       Disable the ALPN TLS extension
 -N, --no-buffer     Disable buffering of the output stream
     --no-keepalive  Disable TCP keepalive on the connection
     --no-npn        Disable the NPN TLS extension
     --no-sessionid  Disable SSL session-ID reusing
     --noproxy <no-proxy-list> List of hosts which do not use proxy
     --ntlm          Use HTTP NTLM authentication
     --ntlm-wb       Use HTTP NTLM authentication with winbind
     --oauth2-bearer <token> OAuth 2 Bearer Token
 -o, --output <file> Write to file instead of stdout
     --pass <phrase> Pass phrase for the private key
     --path-as-is    Do not squash .. sequences in URL path
     --pinnedpubkey <hashes> FILE/HASHES Public key to verify peer against
     --post301       Do not switch to GET after following a 301
     --post302       Do not switch to GET after following a 302
     --post303       Do not switch to GET after following a 303
     --preproxy [protocol://]host[:port] Use this proxy first
 -#, --progress-bar  Display transfer progress as a bar
     --proto <protocols> Enable/disable PROTOCOLS
     --proto-default <protocol> Use PROTOCOL for any URL missing a scheme
     --proto-redir <protocols> Enable/disable PROTOCOLS on redirect
 -x, --proxy [protocol://]host[:port] Use this proxy
     --proxy-anyauth Pick any proxy authentication method
     --proxy-basic   Use Basic authentication on the proxy
     --proxy-cacert <file> CA certificate to verify peer against for proxy
     --proxy-capath <dir> CA directory to verify peer against for proxy
     --proxy-cert <cert[:passwd]> Set client certificate for proxy
     --proxy-cert-type <type> Client certificate type for HTTS proxy
     --proxy-ciphers <list> SSL ciphers to use for proxy
     --proxy-crlfile <file> Set a CRL list for proxy
     --proxy-digest  Use Digest authentication on the proxy
     --proxy-header <header/@file> Pass custom header(s) to proxy
     --proxy-insecure Do HTTPS proxy connections without verifying the proxy
     --proxy-key <key> Private key for HTTPS proxy
     --proxy-key-type <type> Private key file type for proxy
     --proxy-negotiate Use HTTP Negotiate (SPNEGO) authentication on the proxy
     --proxy-ntlm    Use NTLM authentication on the proxy
     --proxy-pass <phrase> Pass phrase for the private key for HTTPS proxy
     --proxy-service-name <name> SPNEGO proxy service name
     --proxy-ssl-allow-beast Allow security flaw for interop for HTTPS proxy
     --proxy-tlsauthtype <type> TLS authentication type for HTTPS proxy
     --proxy-tlspassword <string> TLS password for HTTPS proxy
     --proxy-tlsuser <name> TLS username for HTTPS proxy
     --proxy-tlsv1   Use TLSv1 for HTTPS proxy
 -U, --proxy-user <user:password> Proxy user and password
     --proxy1.0 <host[:port]> Use HTTP/1.0 proxy on given port
 -p, --proxytunnel   Operate through a HTTP proxy tunnel (using CONNECT)
     --pubkey <key>  SSH Public key file name
 -Q, --quote         Send command(s) to server before transfer
     --random-file <file> File for reading random data from
 -r, --range <range> Retrieve only the bytes within RANGE
     --raw           Do HTTP "raw"; no transfer decoding
 -e, --referer <URL> Referrer URL
 -J, --remote-header-name Use the header-provided filename
 -O, --remote-name   Write output to a file named as the remote file
     --remote-name-all Use the remote file name for all URLs
 -R, --remote-time   Set the remote file's time on the local output
 -X, --request <command> Specify request command to use
     --request-target Specify the target for this request
     --resolve <host:port:address> Resolve the host+port to this address
     --retry <num>   Retry request if transient problems occur
     --retry-connrefused Retry on connection refused (use with --retry)
     --retry-delay <seconds> Wait time between retries
     --retry-max-time <seconds> Retry only within this period
     --sasl-ir       Enable initial response in SASL authentication
     --service-name <name> SPNEGO service name
 -S, --show-error    Show error even when -s is used
 -s, --silent        Silent mode
     --socks4 <host[:port]> SOCKS4 proxy on given host + port
     --socks4a <host[:port]> SOCKS4a proxy on given host + port
     --socks5 <host[:port]> SOCKS5 proxy on given host + port
     --socks5-basic  Enable username/password auth for SOCKS5 proxies
     --socks5-gssapi Enable GSS-API auth for SOCKS5 proxies
     --socks5-gssapi-nec Compatibility with NEC SOCKS5 server
     --socks5-gssapi-service <name> SOCKS5 proxy service name for GSS-API
     --socks5-hostname <host[:port]> SOCKS5 proxy, pass host name to proxy
 -Y, --speed-limit <speed> Stop transfers slower than this
 -y, --speed-time <seconds> Trigger 'speed-limit' abort after this time
     --ssl           Try SSL/TLS
     --ssl-allow-beast Allow security flaw to improve interop
     --ssl-no-revoke Disable cert revocation checks (WinSSL)
     --ssl-reqd      Require SSL/TLS
 -2, --sslv2         Use SSLv2
 -3, --sslv3         Use SSLv3
     --stderr        Where to redirect stderr
     --suppress-connect-headers Suppress proxy CONNECT response headers
     --tcp-fastopen  Use TCP Fast Open
     --tcp-nodelay   Use the TCP_NODELAY option
 -t, --telnet-option <opt=val> Set telnet option
     --tftp-blksize <value> Set TFTP BLKSIZE option
     --tftp-no-options Do not send any TFTP options
 -z, --time-cond <time> Transfer based on a time condition
     --tls-max <VERSION> Use TLSv1.0 or greater
     --tlsauthtype <type> TLS authentication type
     --tlspassword   TLS password
     --tlsuser <name> TLS user name
 -1, --tlsv1         Use TLSv1.0 or greater
     --tlsv1.0       Use TLSv1.0
     --tlsv1.1       Use TLSv1.1
     --tlsv1.2       Use TLSv1.2
     --tlsv1.3       Use TLSv1.3
     --tr-encoding   Request compressed transfer encoding
     --trace <file>  Write a debug trace to FILE
     --trace-ascii <file> Like --trace, but without hex output
     --trace-time    Add time stamps to trace/verbose output
     --unix-socket <path> Connect through this Unix domain socket
 -T, --upload-file <file> Transfer local FILE to destination
     --url <url>     URL to work with
 -B, --use-ascii     Use ASCII/text transfer
 -u, --user <user:password> Server user and password
 -A, --user-agent <name> Send User-Agent <name> to server
 -v, --verbose       Make the operation more talkative
 -V, --version       Show version number and quit
 -w, --write-out <format> Use output FORMAT after completion
     --xattr         Store metadata in extended file attributes
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

http请求的时候Header加 Authorization值实现方式 的相关文章

  • 李诗雨的2016

    不诗意的女程序猿不是好厨师 xff0c 大家好 xff0c 我是李诗雨 xff5e 首先 xff0c 要先向各位道个歉 xff0c 其实我也是在csdn上刚拿到 暂住证 的新人 所以有好多功能还不知道 xff0c 今天在手机app上才看到有
  • 使用barrier共享键鼠

    1 安装flatpak 1 1 ubuntu 18及以后 sudo apt install flatpak 1 2 ubuntu18 以前 sudo add apt repository ppa flatpak stablesudo apt
  • OpenCV中访问图像中像素的三种方法

    1 用指针访问像素元 用指针访问像素的这种方法利用的是C语言中的操作符 xff0c 这种方法最快 如 xff1a cv Mat image span class hljs number 100 span span class hljs nu
  • 目标跟踪方法总结

    1 数据集 1 1 OTB 最经典的benchmark xff0c 有2013的OTB50和2015的OTB100OTB包括25 的灰度序列 xff0c 分辨率相对较低http cvlab hanyang ac kr tracker ben
  • SiamFC:基于全卷积孪生网络的目标跟踪算法

    Abstract 本论文提出一种新的全卷积孪生网络作为基本的跟踪算法 xff0c 这个网络在ILSVRC15的目标跟踪视频数据集上进行端到端的训练 我们的跟踪器在帧率上超过了实时性要求 xff0c 尽管它非常简单 xff0c 但在多个ben
  • fusion的理解

    fusion 有很多种模式 xff0c 开始没明白 现在知道fusion 后然后采样 取平均 xff0c 然后恍然大悟
  • rk3288 buildroot 系统编译移植libcurl

    支持https configure prefix 61 pwd buildout host 61 arm buildroot linux CC 61 home gttest work RKLINUX rk3288 v linux build
  • ROS2中IMU话题的发布及可视化

    环境 xff1a Ubuntu 20 04 xff0c ROS2 Foxy 传感器 xff1a 维特智能BWT901CL 代码是从维特智能的示例代码修改的 xff0c 实现基本的加速度 角速度和角度读取 xff0c 发布IMU消息 这个传感
  • vue-admin/vue-element-admin 自动生成表单,数据提交,组件传值

    父组件 xff08 基于element UI xff09 xff1a 弹出层 lt dialogBar dialogData 61 34 dialogData 34 v if 61 34 showUpdateDiaLog 34 64 han
  • 美团2018春招笔试题

    任意一个正整数可以用字符 0 9 表示出来 但是当这些字符每种字符数量有限时 xff0c 可能有些正整数表示不出来 比如有两个 1 xff0c 一个 2 xff0c 能表示出11 12 112等等 xff0c 但是无法表示出10 122 2
  • 如何使用火狐浏览器的Poster插件进行post请求

    不诗意的女程序猿不是好厨师 转载请注明出处 xff1a From 李诗雨 http blog csdn net cjm2484836553 article details 72453907 善假于物者 xff0c 正途也 学会利用身边的工具
  • Grpc常见面试题

    1 grpc四种通信模式 xff1f 答 xff1a 1 xff09 一元 RPC xff1a 客户端发起一个请求 xff0c 服务端给出一个响应 xff0c 然后请求结束 xff1b 2 xff09 服务端流 RPC xff1a 客户端发
  • 读书笔记:无人机控制(五)

    目录 垂直起降飞行器的数学建模与非线性控制RUAV动力学模型刚体动力学 Rigid Body Dynamics 空气动力学力与力矩 Aerodynamics Forces and Torques 非线性分层飞行控制器的设计与稳定性飞行控制器
  • jetson-xavier-nx刷机趟坑记录

    1 sdkmanager刷写系统 sdkmanager启动失败 使用sdkmanager刷机时可能会遇到sdkmanager启动失败的情况 xff0c 界面出现一个 OPT 错误 xff0c 显示 No SDKs are available
  • 虚拟串口软件和串口调试助手的简单使用

    为解决计算机的物理串口个数的限制 xff0c 在进行串口调试实验时 xff0c 应尽可能采用虚拟串口软件 VSPD xff08 Virtual Serial Ports Driver xff09 虚拟串口软件是由Eltima软件公司设计的虚
  • Multi-Object Trackers

    Introduction to Multiple Target Tracking 简单介绍了多目标物跟踪的流程 方法 1 single target tracking STT 单目标跟踪 xff0c 不需要association和assig
  • MATLAB:Introduction to Assignment Methods in Tracking Systems 目标匹配方法

    引言 1 MATLAB R2019b 2 MATLAB中Introduction to Assignment Methods in Tracking Systems介绍了目标匹配的方法 包括背景介绍 xff0c 2 D匹配介绍 xff08
  • Introduction to Using the Global Nearest Neighbor Tracker

    MATLAB R2019b global nearest neighbor GNN tracker xff1a 最近邻 1 目标 Choose the assignment algorithm to associate detections
  • C语言qsort的三种使用

    1 结构体的qsort include lt stdlib h gt include lt stdio h gt struct block int att1 int att2 int cmp const void a const void
  • 编译和使用basalt时出现的报错

    把总结写在前面 请使用GCC9或以上版本编译basalt如果apt安装的fmt版本太低 xff0c 请编译安装高一点版本的fmt使用CLion编译请在配置好运行时的LD LIBRARY PATH 报错一 xff1a github basal

随机推荐