简体   繁体   English

如何在PHP中验证API?

[英]How to Authenticate an API in PHP?

so I'm trying to authenticate an API using a key the developers gave me. 因此,我尝试使用开发人员提供给我的密钥对API进行身份验证。 It's the Propublica Congress API and googling anything on the topic seems to take me to if I was making my own API, how I could create keys to give to users(searching here is the same). 这是Propublica Congress API ,在我自己制作API时,在主题上进行任何搜索似乎都可以带我去了解如何创建要提供给用户的密钥(在此处搜索是相同的)。 I want to authenticate this API so I can get information from it, but nowhere I look seems to have a straightforward answer. 我想对这个API进行身份验证,以便可以从中获取信息,但是我看上去似乎没有一个简单的答案。 How can I authenticate this API using PHP? 如何使用PHP验证此API?

就在文档中,只需将X-API-Key标头和您的api密钥添加到每个请求即可。

You have to email them for the API key... got to the website and click on this button 您必须通过电子邮件发送API密钥...进入网站并单击此按钮

在此处输入图片说明

After you receive the API key from the owner you will then be able to use it in your requests as per the documentation. 从所有者那里收到API密钥后,您将可以根据文档在请求中使用它。

Its depends on how you gonna get data form your API, Suppose if you are using PHP Curl to get data or You are placing request using Javascript you have to create custom header and pass your key through the custom header name X-API-Key . 它取决于您如何从API获取数据,假设您使用的是PHP Curl获取数据,还是使用Javascript发出请求,则必须创建自定义标头,并将键通过自定义标头名称X-API-Key传递。

This is simple PHP Curl example and i have put the custom header 这是简单的PHP Curl示例,我已放置了自定义标头

curl_setopt( $res, CURLOPT_URL, $url );
//curl_setopt( $res, CURLOPT_POST, 3 ); 
//curl_setopt( $res, CURLOPT_POSTFIELDS, $fields );
curl_setopt( $res, CURLOPT_RETURNTRANSFER, true ); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "X-API-Key: $key",
    "customer-header2:value2"
    )); //you can specify multiple custom keys.


$result = curl_exec( $res );
print_r($result);

First of all you have to see how people call APIs using PHP or JS and then you can try authentication and custom headers. 首先,您必须了解人们如何使用PHP或JS调用API,然后可以尝试身份验证和自定义标头。
I would like to recommend you. 我想推荐你。
1) PHP Curl example 1)PHP Curl 示例
2) JQUery AJAX to make request to the any APIs Link 2)JQUery AJAX向任何API 链接发出请求

You need to first explore about how to call APIs using PHP Curl. 您需要首先探索如何使用PHP Curl调用API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM