如何发送 Curl 请求
How to send a Curl request
我有 Instagram 身份验证所需的示例代码。
我不知道如何在 php 中执行此卷曲。下面的步骤是我需要实现的。
第三步:请求 access_token
现在您需要用您在上一步中收到的代码交换访问令牌。为了进行此交换,您只需 POST 此代码以及一些应用程序标识参数到我们的 access_token 端点。这些是必需的参数:
client_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
如果成功,此调用将 return 一个整齐打包的 OAuth 令牌,您可以使用它对 API 进行经过身份验证的调用。为了您的方便,我们还包括刚刚通过身份验证的用户:
{
"access_token": "fb2e77d.47a0479900504cb3ab4a1f626d174d2d",
"user":{
"id": "1574083",
"username": "snoopdogg",
"full_name": "Snoop Dogg",
"profile_picture":“……”
}
}
我是新手。所以我需要帮助
试试这个代码
$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);
$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
希望对您有所帮助
我有 Instagram 身份验证所需的示例代码。 我不知道如何在 php 中执行此卷曲。下面的步骤是我需要实现的。
第三步:请求 access_token
现在您需要用您在上一步中收到的代码交换访问令牌。为了进行此交换,您只需 POST 此代码以及一些应用程序标识参数到我们的 access_token 端点。这些是必需的参数:
client_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
如果成功,此调用将 return 一个整齐打包的 OAuth 令牌,您可以使用它对 API 进行经过身份验证的调用。为了您的方便,我们还包括刚刚通过身份验证的用户:
{ "access_token": "fb2e77d.47a0479900504cb3ab4a1f626d174d2d", "user":{ "id": "1574083", "username": "snoopdogg", "full_name": "Snoop Dogg", "profile_picture":“……” } }
我是新手。所以我需要帮助
试试这个代码
$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);
$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
希望对您有所帮助