← 記事に戻る
# x.comのAPI(FREE)にて自分のユーザー情報を取得するには

おはようございます.x.comのAPI(FREE)にて自分のユーザー情報を取得するにはってググってもv1.1の情報だらけだったので情報を記載します.v2対応です.一部、有料でないと取得できない部分があり返却もエラーで返ってきますが、雛形コードを記載します.

***注意事項***
TwitterOAuthというComposerライブラリを使用しています.
APIなどの値はご自身のAPIに合わしてください.

参考にしたサイト
<https://developer.x.com/en/docs/twitter-api/users/lookup/api-reference/get-users-me>

```
<?php
date_default_timezone_set('Asia/Tokyo');
require_once "../vendor/autoload.php";

use Abraham\TwitterOAuth\TwitterOAuth;

class xMyProfile
{

    public $connection = null;
    public $response = null;

    public function __construct()
    {
        $this->connection = new TwitterOAuth(APIKEY, APISECRET, ACCESSTOKEN, ACCESSTOKENSECRET);
        $this->connection->setApiVersion("2");
        $this->response = $this->connection->get('users/me', [
            'expansions'=>'pinned_tweet_id',
            'tweet.fields'=>implode(',',[
                'attachments',
                'author_id',
                'context_annotations',
                'conversation_id',
                'created_at',
                'edit_controls',
                'entities',
                'geo',
                'id',
                'in_reply_to_user_id',
                'lang',
                'non_public_metrics',
                'public_metrics',
                'organic_metrics',
                'promoted_metrics',
                'possibly_sensitive',
                'referenced_tweets',
                'reply_settings',
                'source',
                'text',
                'withheld'
            ]),
            'user.fields' => implode(',', [
                'created_at',
                'description',
                'entities',
                'id',
                'location',
                'most_recent_tweet_id',
                'name',
                'pinned_tweet_id',
                'profile_image_url',
                'protected',
                'public_metrics',
                'url',
                'username',
                'verified',
                'verified_type',
                'withheld'
            ])
        ]);
        return $this;
    }

    /**
     * プロフィール情報全てを取得
     */
    public function getMyProfile()
    {
        return $this->response;
    }
    /**
     * プロフィールアイコンURLを取得
     */
    public function getIconUrl()
    {
        return $this->response->data->profile_image_url;
    }
}
var_dump((new xMyProfile)->getMyProfile());
//print (new xMyProfile)->getIconUrl();


```



この記事は[Qiita](https://qiita.com/taoka-toshiaki/items/649e03148a10f6a4e4fc)に掲載していた記事になります.

明日へ続く.

 [ ![](https://zip358.com/wp-content/uploads/2026/01/image-30-300x300.png) 半額セールでアドビ様を延長.

 ](https://zip358.com/2026/02/03/%e5%8d%8a%e9%a1%8d%e3%82%bb%e3%83%bc%e3%83%ab%e3%81%a7%e3%82%a2%e3%83%89%e3%83%93%e6%a7%98%e3%82%92%e5%bb%b6%e9%95%b7.html) 

 [ ![](https://zip358.com/wp-content/uploads/2026/01/image-29.png) 何の前触れもなく変えたのは初めてかも.

 ](https://zip358.com/2026/02/02/%e4%bd%95%e3%81%ae%e5%89%8d%e8%a7%a6%e3%82%8c%e3%82%82%e3%81%aa%e3%81%8f%e5%a4%89%e3%81%88%e3%81%9f%e3%81%ae%e3%81%af%e5%88%9d%e3%82%81%e3%81%a6%e3%81%8b%e3%82%82.html) 

 [ ![](https://zip358.com/wp-content/uploads/2023/01/you-300x300.png) 跳ねたいサイトで跳ねたい

 ](https://zip358.com/2026/02/01/%e8%b7%b3%e3%81%ad%e3%81%9f%e3%81%84%e3%82%b5%e3%82%a4%e3%83%88%e3%81%a7%e8%b7%b3%e3%81%ad%e3%81%9f%e3%81%84.html) 

 [ ![](https://zip358.com/wp-content/uploads/2026/01/image-36.png) 映画、機動戦士ガンダム 閃光のハサウェイ キルケーの魔女

 ](https://zip358.com/2026/01/31/%e6%98%a0%e7%94%bb%e3%80%81%e6%a9%9f%e5%8b%95%e6%88%a6%e5%a3%ab%e3%82%ac%e3%83%b3%e3%83%80%e3%83%a0-%e9%96%83%e5%85%89%e3%81%ae%e3%83%8f%e3%82%b5%e3%82%a6%e3%82%a7%e3%82%a4-%e3%82%ad%e3%83%ab%e3%82%b1.html) 

 [ ![](https://zip358.com/wp-content/uploads/2024/09/image-35.png) AI(人工知能)を使ったスタートアップが急増中

 ](https://zip358.com/2026/01/30/ai%ef%bc%88%e4%ba%ba%e5%b7%a5%e7%9f%a5%e8%83%bd%ef%bc%89%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%9f%e3%82%b9%e3%82%bf%e3%83%bc%e3%83%88%e3%82%a2%e3%83%83%e3%83%97%e3%81%8c%e6%80%a5%e5%a2%97%e4%b8%ad.html)