【问题标题】:Can't understand Instagram API code snippet无法理解 Instagram API 代码片段
【发布时间】:2019-07-12 11:13:16
【问题描述】:

我想做一个项目来了解 API 集成在 Web 应用程序中的工作原理。所以,我选择了 instagram 的登录 api。如果有人选择通过 instagram 登录我的网站,它的基本作用是存储用户信息,如用户名等。

我不知道从哪里开始,所以我开始查看其他人的代码,他们已经完成了。所以有一个函数叫getAccessTokenAndUserDetails() 我不明白。这是代码sn-p:

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 关联数组之外,这段代码对我来说是法语。需要帮助。

【问题讨论】:

  • @Dharman 我读了这篇文章。谢谢。知道这一点很有用

标签: php instagram-api php-curl


【解决方案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&para2=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);
}

【讨论】:

  • 那么get、post和curl有什么区别呢?由于 GET 和 POST 也用于发出 Web 请求
  • GET 和 POST 是 http 请求的类型。 Curl 是一种工具,可以发出两种类型的 http 请求,甚至更多。
  • 你能举个例子说明它是如何工作的吗?
  • OP 就是一个例子。这是一个关于 curl 的教程,它还以简单的方式解释了 http 协议的重要部分:curl.haxx.se/docs/httpscripting.html
猜你喜欢
  • 2012-10-24
  • 2012-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-21
  • 2015-08-22
相关资源
最近更新 更多