PHP如何使用授权头 [英] How to use authorization header PHP

查看:26
本文介绍了PHP如何使用授权头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用授权标头来使用 vimeo API.

I am trying to use an authorization header in order to use the vimeo API.

它告诉我做这个 'Authorization: basic ' + base64(client_id + ':' + client_secret) ,这是我可以做的.

It tells me to do this 'Authorization: basic ' + base64(client_id + ':' + client_secret) , which is something I can do.

但是互联网上没有任何地方告诉我我实际上用这段代码做了什么?它不是 PHP,但它是否在 PHP 文件中?如果是这样,那么在存储它之后我在它上面使用什么功能?它是否在 htaccess 文件中?

But nowhere on the internet does it tell me what I actually do with this code? It is not PHP, but does it go in a PHP file? If so then what function do I use on it after storing it? Does it go in an htaccess file?

令人遗憾的是,任何和所有在线文档都如此糟糕.

It is really sad how terrible any and all online documentation is on this.

总而言之,基本上我要说的是向我展示代码

To summarize, basically what I'm saying is SHOW ME THE CODE

推荐答案

$api_url = 'http://myapiurl';

$client_id = 'myclientid';
$client_secret = 'myclientsecret';

$context = stream_context_create(array(
    'http' => array(
        'header' => "Authorization: Basic " . base64_encode("$client_id:$client_secret"),
    ),
));

$result = file_get_contents($api_url, false, $context);

文档链接:

对于更复杂的请求,您可以使用 cURL,但是该库的 PHP 实现是一团糟,我更喜欢尽可能避免它.Guzzle 是一个抽象了这里很多复杂性的库.

For more complex requests, you can use cURL, but the library's PHP implementation is a mess and I prefer to avoid it when I can. Guzzle is a library that abstracts a lot of the complexities here.

这篇关于PHP如何使用授权头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆