← 記事に戻る
# x.gdのURL短縮機能を利用するためのコードを作りましたので、お裾分け.

おはようございます.x.gdのURL短縮機能を利用するためのコードを作りましたので、お裾分けです、コードは下記になります.ドキュメントをぱぱっと見て作った感じなので不具合などが潜んでいるかもしれないけど恐らく大丈夫そうという事で木曜日の早朝にXでコードをシェアした形になります.

なお、x.gdからAPIの発行を行う作業をお忘れなく(こちらから[APIを発行](https://x.gd/view/developer)).

因みに自分はt2t.jpというドメインを私有していて、こちらで自分専用の短縮URLを発行出来るようにしています.この頃は自分が作った短縮URLをあまり使ってないので、こちらをサービス化しようかな等と考えていたりします.

因みにどうするかは未定ですけどね.もしみんなが使用出来るようにしたら、このサイトでも告知します.

```
<?php
require 'config.php';

class ShortUrl
{

    public $apiKey = null;
    public $endPoint = 'https://xgd.io/V1/shorten?';

    public function __construct($api_key = null)
    {
        $this->apiKey = $api_key;
        return $this;
    }

    public function getShortUrl($url = null, $shortid = null, $analytics = true, $filterbots = false)
    {
        if (empty($this->apiKey)) {
            return json_encode(['status' => 0, 'message' => 'api-keyがありません']);
        }
        if (empty($url)) {
            return json_encode(['status' => 0, 'message' => 'URLがありません']);
        }

        if (isset($url) && !preg_match('/^http[s]?:\/\/(www\.)?/', $url)) {
            return json_encode(['status' => 0, 'message' => 'URLが無効です']);
        }

        if (isset($shortid) && !preg_match('/^([0-9a-zA-Z_]{6,15})$/', $shortid)) {
            return json_encode(['status' => 0, 'message' => '短縮コードが無効です']);
        }

        if (!is_bool($analytics)) {
            $analytics = true;
        }

        if (!is_bool($filterbots)) {
            $filterbots = false;
        }

        $parameter = [
            'key' => $this->apiKey,
            'url' => $url,
            'shortid' => $shortid,
            'analytics' => $analytics ? 'true' : 'false',
            'filterbots' => $filterbots ? 'true' : 'false'
        ];

        $response = @file_get_contents($this->endPoint . http_build_query($parameter));
        return $response;
    }
}

if($argv[1]){
    $url = $argv[1];
    $shortid = isset($argv[2]) ? $argv[2] : null;
    $analytics = isset($argv[3]) && $argv[3] === 'true' ? true : false;
    $filterbots = isset($argv[4]) && $argv[4] === 'true' ? true : false;   
    echo (new ShortUrl(APIKEY))->getShortUrl($url, $shortid, $analytics, $filterbots);
}
```



明日へ続く

 [ ![](https://zip358.com/wp-content/uploads/2026/01/image-31.png) サイトを分離した話を黙々と書いていきます.

 ](https://zip358.com/2026/02/04/%e3%82%b5%e3%82%a4%e3%83%88%e3%82%92%e5%88%86%e9%9b%a2%e3%81%97%e3%81%9f%e8%a9%b1%e3%82%92%e9%bb%99%e3%80%85%e3%81%a8%e6%9b%b8%e3%81%84%e3%81%a6%e3%81%84%e3%81%8d%e3%81%be%e3%81%99.html) 

 [ ![](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)