This is why I’m using JS as example, so we can easily quickly generate the AUTH token before our own call. So for each call I make, I need to regenerate this. My example API only keeps my auth-token alive for 2 weeks AND will only allow 3 auth-tokens at a time. With this auth token, I can start making normal API calls like POST and GET. 'client_secret' => 'btHTWVNMUATHEnF-XXX-2nQabKcKVo3VXtU',Ĭurl_setopt($curl, CURLOPT_POSTFIELDS, $auth_data) Ĭurl_setopt($curl, CURLOPT_RETURNTRANSFER, 1) Ĭurl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC) 'client_id' => 'XBnKaywRCrj05mM-XXX-6DXuZ3FFkUgiw45', In this tutorial, I’ll be calling this file api/auth.php $curl = curl_init() Here is how I can generate and use my auth-token, based on the cURL script of my part-1 tutorial. In my example, I can generate an auth-token by posting my API client ID, client_secret and a login type to their API Auth file. Normally the API docs should explain how you can generate their auth-token. We need to generate this auth-token first, before we are allowed to make API calls. Some API’s only allow POST or GET requests if you use an auth-token. If I’m linking to, this means I’m making a call the the external API. If you see me linking to, this means I created the file myself, on my own server. Therefore I create my API calls in my own php files on the side, and will target these files using ES6 async/fetch from within my page. As I need to make an ‘authentication’ api call first before our initial GET call. Heads up: This time, I will be using Javascript for more flexibility. This is part 2 of how to connect to an API using cURL in php, as I received a lot of questions on how to connect if the API requires authentication (utoken) first. If you’re here because you want to connect your php code to an external API, please check my cURL api-calls with php tutorial first.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |