WebAPIの作り方、考え方です?。サンプルコードもありますよ。

20211224

Logging

昨日から風邪を引いてしまいました…。今日も体調が優れない状態ですが、昨日よりはマシになっています、因みに風邪というよりも腸と胃に菌がはいってしまって、それによる発熱です?。

さて、今回はPHP言語でWebAPIの作りましたので、ご報告致します、どんなAPIかというと生年月日とカウントしたい歳をPOSTすると、現在の年齢、今まで生きてきた日数、カウント日数がレスポンス(返却)されます。

【JavaScript入門 #8】WebAPIを叩いてみよう!async await構文を使うと簡単!【ヤフー出身エンジニアの入門プログラミング講座】

PHPコードは下記の通りになります。適当に作ったので間違っている箇所があるかもしれませんが、そこはご愛嬌でお願いできますでしょうか?、また、WebAPIの叩き方はご自身でお考えくださいませ。
サーバーに負荷が増したらWEBAPIは閉じます。

尚、WEBAPIのURLはこちらになります。

https://zip358.com/api/age/v1/type1/

<?php
header('Access-Control-Allow-Origin: *');
date_default_timezone_set('Asia/Tokyo');
$birth_date = (string)$_POST["birth_date"];
$max_age = (int)$_POST["point_age"];

/**
 * @param string $birth_date
 * @return string|false
 */
function check1($birth_date = ""): bool
{
    $flg = false;
    $str_date = explode("/", $birth_date);
    if (count($str_date) === 3) {
        $flg = true;
        if (!((int)$str_date[0] >= 1000)) {
            $flg = false;
        }
        if(((int)$str_date[0] > (int)date("Y"))){
            $flg = false;
        }
        if (!((int)$str_date[1] >= 1 && (int)$str_date[1] <= 12)) {
            $flg = false;
        }
        if ($flg) {
            if ((int)$str_date[1] === 2) {
                if (!((int)$str_date[2] >= 1 && (int)$str_date[2] <= 28)) {
                    $flg = false;
                }
                if ((int)$str_date[0] % 4 === 0) {
                    $flg = true;
                    if (!((int)$str_date[2] >= 1 && (int)$str_date[2] <= 29)) {
                        $flg = false;
                    }
                    if ((int)$str_date[0] % 100 === 0) {
                        $flg = true;
                        if (!((int)$str_date[2] >= 1 && (int)$str_date[2] <= 28)) {
                            $flg = false;
                        }
                        if ((int)$str_date[0] % 400 === 0) {
                            $flg = true;
                            if (!((int)$str_date[2] >= 1 && (int)$str_date[2] <= 29)) {
                                $flg = false;
                            }
                        }
                    }
                }
            } else {
                $last_day = [4, 6, 9, 11];
                if (array_search((int)$str_date[1], $last_day, false)!== false) {
                    if (!((int)$str_date[2] >= 1 && (int)$str_date[2] <= 30)) {
                        $flg = false;
                    }
                } else {
                    if (!((int)$str_date[2] >= 1 && (int)$str_date[2] <= 31)) {
                        $flg = false;
                    }
                }
            }
        }
    }
    return $flg;
}

/**
 * @param int $age
 * @return string|false
 */
function check2($age = 0): bool
{
    $flg = true;
    if ($age < 0) {
        $flg = false;
    }
    return $flg;
}


/**
 * @param string $birth_date
 * @param string $maxage
 * @return string $reslut
 */
function sumup($birth_date, $maxage)
{
    $reslut = [];
    $birth_date_array = explode("/", $birth_date);
    $birth_date = sprintf("%04d%02d%02d", $birth_date_array[0], $birth_date_array[1], $birth_date_array[2]);
    $today = date('Ymd');
    $age = floor(($today - $birth_date) / 10000);
    $day1 = new DateTime("{$birth_date_array[0]}-{$birth_date_array[1]}-{$birth_date_array[2]}");
    $day2 = new DateTime();    
    $interval1 = $day1->diff($day2, true);
    $baseday =  (int)($interval1->format('%a'));
    if ((int)$maxage <= (int)$age) {
        $pointday = 0;
    } else {
        $maxage--;
        $day3 = new DateTime((date('Y') + ($maxage - $age)) . "-{$birth_date_array[1]}-{$birth_date_array[2]}");
        $interval2 = $day2->diff($day3, true);
        $pointday = (int)($interval2->format('%a'))+1;
    }


    $reslut = [
        [
            "result" => "success",
            "age"=>$age ."歳",
            "baseday" => $baseday . "日(生きてきた日数)",
            "pointday" => $pointday . "日(" .($maxage +1). "歳まであと)"
        ]
    ];
    return json_encode($reslut);
}

if (!check1($birth_date)) {
    print json_encode([
        [
            "result" => "error",
            "error" => "string is invalid1"
        ]
    ]);
} elseif (!check2($max_age)) {
    print json_encode([
        [
            "result" => "error",
            "error" => "string is invalid2"
        ]
    ]);
} else {
    print sumup($birth_date, $max_age);
}

タグ

39, Access-Control-Allow-Origin, API, header, lt, php, POST, url, WebApi, お願い, カウント, コード, こちら, ご報告, ご愛嬌, ご自身, サーバー, サンプル, そこ, それ, まし, レスポンス, 下記, 今回, 今日, 体調, 作り方, 叩き, 年齢, 日数, 昨日, , 状態, 現在, 生年月日, 発熱, 箇所, 考え方, , , , 言語, 負荷, 返却, 通り, 適当, 風邪,