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

请求协助编写适配Instagram新规则的完整头像查看PHP脚本

Solution: PHP Code for WordPress to Fetch Instagram Full-Size Avatars

I’ve converted your manual two-step process into a reusable PHP function tailored for WordPress. This handles fetching the user ID first, then pulling the full-size avatar URL, with proper error handling and WordPress best practices:

function get_instagram_full_size_avatar($username) {
    // Step 1: Grab the user ID from Instagram's public profile endpoint
    $profile_endpoint = "https://www.instagram.com/{$username}/?_a=1";
    
    // Set a realistic user agent to avoid being blocked (Instagram flags generic bots)
    $request_args = array(
        'headers' => array(
            'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36'
        ),
        'timeout' => 10
    );
    
    // Fetch the profile data
    $profile_response = wp_remote_get($profile_endpoint, $request_args);
    
    // Handle request failures
    if (is_wp_error($profile_response)) {
        return false;
    }
    
    $profile_body = wp_remote_retrieve_body($profile_response);
    $profile_data = json_decode($profile_body, true);
    
    // Make sure we can extract the user ID
    if (!isset($profile_data['graphql']['user']['id'])) {
        return false;
    }
    
    $user_id = $profile_data['graphql']['user']['id'];
    
    // Step 2: Fetch the full-size avatar using the user ID
    $avatar_endpoint = "https://i.instagram.com/api/v1/users/{$user_id}/info/";
    
    $avatar_response = wp_remote_get($avatar_endpoint, $request_args);
    
    if (is_wp_error($avatar_response)) {
        return false;
    }
    
    $avatar_body = wp_remote_retrieve_body($avatar_response);
    $avatar_data = json_decode($avatar_body, true);
    
    // Extract the highest-res avatar URL (with fallback)
    if (isset($avatar_data['user']['hd_profile_pic_url_info']['url'])) {
        return $avatar_data['user']['hd_profile_pic_url_info']['url'];
    } elseif (isset($avatar_data['user']['profile_pic_url_hd'])) {
        // Fallback in case Instagram changes the response structure
        return $avatar_data['user']['profile_pic_url_hd'];
    } else {
        return false;
    }
}

How to Use This in Your WordPress Site:

Call the function wherever you need to display the avatar—for example, in your theme’s template files or a custom plugin:

$instagram_username = "target_username";
$full_size_avatar = get_instagram_full_size_avatar($instagram_username);

if ($full_size_avatar) {
    // Output the image safely with WordPress's esc_url
    echo '<img src="' . esc_url($full_size_avatar) . '" alt="Instagram Profile Avatar">';
} else {
    echo 'Sorry, we couldn’t fetch the Instagram avatar.';
}

Key Things to Keep in Mind:

  • Avoid Getting Blocked: Instagram limits requests from non-official sources. Add caching with wp_cache_set and wp_cache_get to store the avatar URL for 24–48 hours instead of fetching it every time.
  • User Agent Updates: If the function stops working, try updating the User-Agent string to match a modern browser’s.
  • Endpoint Changes: These are Instagram’s private, unendorsed endpoints—they can change at any time. If the function breaks, check the JSON responses to see if the structure has been updated.

内容的提问来源于stack exchange,提问作者Nolan Anderson

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 08:06:53