so I'm trying to authenticate an API using a key the developers gave me. 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). I want to authenticate this API so I can get information from it, but nowhere I look seems to have a straightforward answer. How can I authenticate this API using PHP?
-
What you are using to place API request, like Javascript AJAX, PHP Curl or what you are using to get data?? – BetaDev Mar 27 '17 at 14:39
3 Answers
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
.
This is simple PHP Curl example and i have put the custom header
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.
I would like to recommend you.
1) PHP Curl example
2) JQUery AJAX to make request to the any APIs Link
You need to first explore about how to call APIs using PHP Curl.
It's right there in the documentation, just add a X-API-Key
header with your api key to each request.

- 32,252
- 11
- 87
- 108
-
Maybe I'm just over my head ... but I honestly don't know what adding an that to the header means. Adding ?X-API-Key=key to the URL still tells me it's forbidden to me. – PoasterToaster Mar 27 '17 at 14:39
-
That's not what he means. You should post the code, you want ot use to get a more specific answer. Do you use curl for that? then have a look here: http://stackoverflow.com/questions/8115683/php-curl-custom-headers – Oliver Mar 27 '17 at 14:50
You have to email them for the API key... got to the website and click on this button
After you receive the API key from the owner you will then be able to use it in your requests as per the documentation.

- 956
- 7
- 21