php 如何使用授权头PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30599506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:54:51  来源:igfitidea点击:

How to use authorization header PHP

phpapioauthauthorizationvimeo

提问by Sam Alexander

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

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

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

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

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?

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

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

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

回答by Mikkel

$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);

Documentation links:

文档链接:

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. Guzzleis a library that abstracts a lot of the complexities here.

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

回答by Dashron

Vimeo highly recommends you do not write these authentication systems yourself, but use the official libraries: https://github.com/vimeo/vimeo.php.

Vimeo 强烈建议您不要自己编写这些认证系统,而是使用官方库:https: //github.com/vimeo/vimeo.php

If you are looking for a custom PHP integration, it varies based on the way you make HTTP requests. guzzle and curl are both http request libraries, with their own ways of setting headers (http://guzzle.readthedocs.org/en/latest/request-options.html#headersand PHP cURL custom headers)

如果您正在寻找自定义 PHP 集成,它会根据您发出 HTTP 请求的方式而有所不同。guzzle 和 curl 都是 http 请求库,它们有自己的设置标题的方式(http://guzzle.readthedocs.org/en/latest/request-options.html#headersPHP cURL 自定义标题

As for base64 encoding your tokens, use the method base64_encode(http://php.net/manual/en/function.base64-encode.php)

至于对令牌进行 base64 编码,请使用该方法base64_encodehttp://php.net/manual/en/function.base64-encode.php