我一直在寻找一个项目来了解API集成如何在Web应用程序中工作。因此,我选择了instagram的登录api。它的基本作用是存储用户信息,例如用户名等。如果有人选择通过instagram登录到我的网站。
我不知道从哪里开始,所以我开始阅读已经做过的其他人的代码。因此有一个我不理解的名为getAccessTokenAndUserDetails()
的函数。这是代码片段:
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);
}
除了正在设置的$postFields
关联数组之外,此代码对我来说还是法语。需要帮助。
答案 0 :(得分:1)
curl是用于发出Web请求的命令行工具。此功能正在配置并使用此工具来访问远程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);
}