You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP调用Instagram API仅获ID和用户名,如何获取完整用户资料

问题:调用Instagram API仅能获取用户ID和用户名,无法获取完整资料

当前调用Instagram API后仅能拿到如下用户数据:

( [id] => 123456 [username] => abc123)

需要获取用户全名、头像、邮箱等完整资料,当前实现代码如下:

$code = $this->getRequest()->getParam('code');
$instaAppId = $this->modelDataProvider->getAppApiKey(self::SOCIAL_TYPE);
$instaAppSecret = $this->modelDataProvider->getAppApiSecret(self::SOCIAL_TYPE);
$instaRedirectUrl = $this->modelDataProvider->getRedirectCallbackUrls(self::SOCIAL_TYPE);
$setAccessToken = $this->getAccessTokenValue($instaAppId, $instaRedirectUrl, $instaAppSecret, $code);
$AccessToken = $setAccessToken['access_token'];
$instauserinfo = $this->getInstagramUserProfile($AccessToken);
print_r($instauserinfo);


protected function getAccessTokenValue($instaAppId, $instaRedirectUrl, $instaAppSecret, $code)
{
    $token = [];
    try {
        $request = 'client_id='. $instaAppId . '&redirect_uri=' . $instaRedirectUrl . '&client_secret=' . $instaAppSecret . '&code='. $code . '&grant_type=authorization_code';
        $this->curlClient->setOptions([
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_TIMEOUT => 60,
            CURLOPT_POSTFIELDS => $request,
            CURLOPT_HEADER => false,
            CURLOPT_VERBOSE => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_HTTPHEADER => [
                'Content-Type: application/x-www-form-urlencoded',
            ]
        ]);
        $this->curlClient->get('https://api.instagram.com/oauth/access_token');
        $token = $this->json->unserialize($this->curlClient->getBody());
    } catch (\Exception $exception) {
        $token['errorMessage'] = $exception->getMessage();
    }
    return $token;
}

protected function getInstagramUserProfile($AccessToken)
{
    $instaprofileid = [];
    try {
        $this->curlClient->setOptions([
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_TIMEOUT => 60,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_HTTPHEADER => [
                'Authorization: Bearer ' . $AccessToken,
            ]
        ]);
        $this->curlClient->get('https://graph.instagram.com/me/?fields=id,name,username,profile_picture_url&access_token='. $AccessToken);
        $instaprofileid = $this->json->unserialize($this->curlClient->getBody());
    } catch (\Exception $exception) {
        $this->getResponse()->setBody($exception->getMessage());
    }
    return $instaprofileid;
}

解决步骤

1. 确认授权阶段的权限申请

要获取更多用户信息,必须在用户授权跳转时指定所需权限:

  • 获取邮箱需要user_email权限,获取头像、全名需要user_profile权限
  • 调整授权URL,添加scope参数:
    https://api.instagram.com/oauth/authorize?client_id=你的应用ID&redirect_uri=你的回调地址&scope=user_profile,user_email&response_type=code
    

2. 修正用户资料请求的字段与接口

  • 当前getInstagramUserProfile方法的请求URL中,既在header带了Bearer Token,又在URL里重复加了access_token参数,建议去掉URL中的重复参数
  • 补充需要的字段,比如email,修正后的请求URL:
    https://graph.instagram.com/me?fields=id,username,name,profile_picture_url,email
    

3. 检查应用审核状态

  • 若要获取email这类敏感信息,你的Instagram应用必须通过Meta的应用审核,否则无法返回该字段
  • 确保应用的隐私政策、使用场景说明符合Meta的审核要求,审核通过后才能获取对应权限的字段。

4. 代码调整示例

修改getInstagramUserProfile中的请求代码:

$this->curlClient->get('https://graph.instagram.com/me?fields=id,username,name,profile_picture_url,email');

内容的提问来源于stack exchange,提问作者Rubel Parvaz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 14:52:44