I was looking to do a project to understand how API integrations work in web applications. So, i chose instagram's login api. What it basically does is store user info like username etc. if somebody chooses to log into my website through instagram.
I didn't know where to begin so i started to go through other people's code, who had already done it. So there is a function called getAccessTokenAndUserDetails()
that I don't understand. And here is the code snippet:
public function getAccessTokenAndUserDetails($code) {
$postFields = array(
"client_id" => $this->clientID,
"client_secret" => $this->clientSecret,
"grant_type" => "authorization_code",
"redirect_uri" => $this->redirectURI,
"code" => $code
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
"https://api.instagram.com/oauth
/access_token");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
Apart from the $postFields
associative array that is being set, this code is french to me. Need help.
curl is a command line tool for making web requests. This function is configuring and using this tool to access the remote api.
public function getAccessTokenAndUserDetails($code) {
// These are the parameters that the api needs to process the request.
// You can think of them like the information filled out by a human on a webform.
$postFields = array(
"client_id" => $this->clientID,
"client_secret" => $this->clientSecret,
"grant_type" => "authorization_code",
"redirect_uri" => $this->redirectURI,
"code" => $code
);
// Gets an instance of the curl tool
$ch = curl_init();
// curl_setopt configures the curl tool options
// all of the options can be found in the docs:
// https://www.php.net/manual/en/book.curl.php
// https://www.php.net/manual/en/function.curl-setopt.php
// The URL to fetch. This can also be set when initializing a session with curl_init().
curl_setopt($ch, CURLOPT_URL,
"https://api.instagram.com/oauth
/access_token");
// TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it directly.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 1 to check the existence of a common name in the SSL peer certificate.
// 2 to check the existence of a common name and also verify that it matches the hostname provided.
// 0 to not check the names. In production environments the value of this option should be kept at 2 (default value).
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
// FALSE to stop cURL from verifying the peer's certificate.
// Alternate certificates to verify against can be specified with the CURLOPT_CAINFO option
// or a certificate directory can be specified with the CURLOPT_CAPATH option.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// TRUE to do a regular HTTP POST.
// This POST is the normal application/x-www-form-urlencoded kind, most commonly used by HTML forms.
curl_setopt($ch, CURLOPT_POST, 1);
// The full data to post in a HTTP "POST" operation.
// To post a file, prepend a filename with @ and use the full path.
// The filetype can be explicitly specified by following the filename with the type in the format ';type=mimetype'.
// This parameter can either be passed as a urlencoded string like 'para1=val1¶2=val2&...' or as an array with
// the field name as key and field data as value. If value is an array, the Content-Type header
// will be set to multipart/form-data.
// As of PHP 5.2.0, value must be an array if files are passed to this option with the @ prefix.
// As of PHP 5.5.0, the @ prefix is deprecated and files can be sent using CURLFile.
// The @ prefix can be disabled for safe passing of values beginning with @ by setting the CURLOPT_SAFE_UPLOAD option to TRUE.
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
// actually visits the site and stores the response in $response
$response = curl_exec($ch);
// close the connection and release the memory used by the curl tool
curl_close($ch);
// assumes that the response was JSON encoded, so decodes it into a more useful PHP format and returns the decoded value.
return json_decode($response, true);
}