【问题标题】:How to send a Curl request [duplicate]如何发送 Curl 请求 [重复]
【发布时间】:2017-11-14 17:25:56
【问题描述】:

我有 Instagram 身份验证所需的示例代码。 我不知道如何在 php 中执行此 curl。下面的步骤是我需要实现的。

第三步:请求 access_token

现在您需要将在上一步中收到的代码交换为访问令牌。为了进行这种交换,您只需将此代码连同一些应用程序标识参数一起发布到我们的 access_token 端点。这些是必需的参数:

client_id:您的客户 ID client_secret:您的客户端密码 grant_type:authorization_code 目前是唯一支持的值 redirect_uri:你在授权请求中使用的redirect_uri。注意:这必须与授权请求中的值相同。 代码:您在授权步骤中收到的确切代码。 这是一个示例请求:

curl -F 'client_id=CLIENT_ID' \
-F 'client_secret=CLIENT_SECRET' \
-F 'grant_type=authorization_code' \
-F 'redirect_uri=AUTHORIZATION_REDIRECT_URI' \
-F 'code=CODE' \
https://api.instagram.com/oauth/access_token

如果成功,此调用将返回一个整齐打包的 OAuth 令牌,您可以使用该令牌对 API 进行经过身份验证的调用。为方便起见,我们还包括刚刚进行身份验证的用户:

{ “access_token”:“fb2e77d.47a0479900504cb3ab4a1f626d174d2d”, “用户”:{ “id”:“1574083”, "用户名": "snoopdogg", "full_name": "Snoop Dogg", "profile_picture": "..." } }

我是新手。所以我需要帮助

【问题讨论】:

标签: php instagram


【解决方案1】:
$url = 'Enter the user here';
        $myvars = 'secret=' . $secretKey. '&remoteip=' . $ip;//These are parameters

        $ch = curl_init($url);//initialise
        curl_setopt($ch, CURLOPT_POST, 1);//post request
        curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        $response = curl_exec($ch);//Execute curl request
        $result = json_decode($response);//decode the json response

希望对你有帮助

【讨论】:

    【解决方案2】:

    试试这个代码

    $client_id = 'YOUR CLIENT ID';
        $client_secret ='YOUR CLIENT SECRET';
            $redirect_uri = 'YOUR REDIRECT URI';
        $code ='Enter your code manually';
    
        $url = "https://api.instagram.com/oauth/access_token";
        $access_token_parameters = array(
            'client_id'                =>     $client_id,
            'client_secret'            =>     $client_secret,
            'grant_type'               =>     'authorization_code',
            'redirect_uri'             =>     $redirect_uri,
            'code'                     =>     $code
        );
    
    $curl = curl_init($url);    // we init curl by passing the url
        curl_setopt($curl,CURLOPT_POST,true);   // to send a POST request
        curl_setopt($curl,CURLOPT_POSTFIELDS,$access_token_parameters);   // indicate the data to send
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);   // to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);   // to stop cURL from verifying the peer's certificate.
        $result = curl_exec($curl);   // to perform the curl session
        curl_close($curl);   // to close the curl session
    
         var_dump($result);
    

    【讨论】:

    • 你真是个天才……它工作得很好……TY
    猜你喜欢
    • 1970-01-01
    • 2018-04-12
    • 2020-04-05
    • 2018-08-13
    • 2018-04-23
    • 2020-10-10
    • 2013-07-01
    • 2023-03-25
    • 1970-01-01
    相关资源
    最近更新 更多