LoginSignup
12
12

More than 5 years have passed since last update.

ポケモンGOの配信チェック(PHP版)

Last updated at Posted at 2016-07-21

はじめに

先週くらいまではポケモンGOなんて特に興味なかったのですが、
今週くらいになりほぼ毎日ポケモンGOの話題を聞くようになり、、気付いたら日本での配信を心待ちにしていました。。
すでに配信チェックされている方がおられたので、それを真似てPHP版で作りました。
参考:【スタダ】ポケモンGOがリリースされたら電話掛かってくるスクリプト

コード

pokemon.php
<?php

namespace Pokemon;

class PokemonNotification
{
    // mt=8はiOSアプリを意味する
    const APP_URL = 'https://itunes.apple.com/jp/app/apple-store/id1094591345?mt=8';

    // 配信されているかをチェックする文字列
    const CHECK_STRING = 'カスタマーレビュ';

    /*
     * メイン処理
     *
     */
    function exec()
    {
        try {
            // iTunesで確認
            $result = self::request('GET', self::APP_URL);
            $isDelivery = self::isDelivery($result['response']);

            // 配信中かそうでないかでメッセージを出し分け
            if ($isDelivery) {
                $message = 'ポケモンGO!!!!!! ';
            } else {
                $message = 'ポケモンGOはまだ配信されてません。。 ';
            }

            echo $message . "\n";

        } catch (\Exception $e) {
            echo $e->getMessage(). "\n";
        }
    }

    /*
     * リクエスト
     *
     * @param string $method
     * @param string $url
     * @param string $data
     * @return array $result
     */
    private static function request($method, $url, $data = '')
    {
        $result = array();

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

        // メソッドの振り分け
        if ($method === 'GET') {
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        } elseif ($method === 'POST') {
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        }

        // データがあればセット
        if (!empty($data)) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }

        // リクエスト
        $response = curl_exec($ch);
        if ($response === false) {
            throw new \Exception('curl error');
        }

        $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        $result['response'] = $response;
        $result['status'] = $status;

        return $result;
    }

    /*
     * アプリが配信されているかチェック
     *
     * @param string $data
     * @return bool $result
     */
    private static function isDelivery($data)
    {
        $result = false;

        if (strpos($data, self::CHECK_STRING) !== false) {
            $result = true;
        }

       return $result;
    }
}

$pokemon = new PokemonNotification();
$pokemon->exec();

実行

$ php pokemon.php
ポケモンGOはまだ配信されてません。。

おわりに

これをcronで1分毎とかに実行すればほぼリアルタイムにチェックできるかと思います。
あとは配信されたら何かしらに通知とかすればバッチリです。

12
12
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
12