Last active
October 23, 2022 03:46
-
-
Save DaisukeDaisuke/0d934cde7cf72b91be5c2f6ff366eef4 to your computer and use it in GitHub Desktop.
GithubAPI Request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class http{ | |
| protected const QIITA_API_URL = "https://qiita.com/api/v2"; | |
| protected const GITHUB_API_URL = "https://api.github.com"; | |
| private static string $githubToken; | |
| private static function getGithubToken() : string{ | |
| return self::$githubToken; | |
| } | |
| public static function setGithubToken(string $githubToken) : void{ | |
| self::$githubToken = $githubToken; | |
| } | |
| public static function cleanGithubToken() : void{ | |
| self::$githubToken = str_repeat("a", 50); | |
| } | |
| public static function getQiita(string $url, $data = false, $request = false, string $token = "testtoken"): array{ | |
| $url = str_replace(self::QIITA_API_URL, "", $url); | |
| return self::get(self::QIITA_API_URL.$url, $data, $request, "Bearer ".$token); | |
| } | |
| public static function getGithub(string $url, $data = false, $request = false) : array{ | |
| $url = str_replace(self::GITHUB_API_URL, "", $url); | |
| return self::get(self::GITHUB_API_URL.$url, $data, $request, "token ".self::getGithubToken()); | |
| } | |
| /** | |
| * @param $url /repos/DaisukeDaisuke/testrepos0001/git/trees/master?recursive=true, /repos/DaisukeDaisuke/testrepos0001/git/refs/heads/master | |
| * @param array<mixed, mixed>|false $data パラメータ、「false」の場合、「get」リクエストを発行、「配列」の場合、「post」リクエストの発行を実施します。 | |
| * @param string|false $request カスタムリクエスト「PATCH」「DELETE」「PUT」 | |
| * @param string $token github トークン ファイルより直接取得します事をお勧めします... | |
| * @return array<mixed, mixed> jsonデコードを実施した本文 | |
| * @throws RuntimeException | |
| */ | |
| public static function get(string $url, $data = false, $request = false, string $token = "testtoken"){ | |
| if($request !== false){ | |
| var_dump($request.": ".$url); | |
| }else if($data !== false){ | |
| var_dump("POST: ".$url); | |
| }else{ | |
| var_dump("GET: ".$url); | |
| } | |
| $curl = curl_init($url); | |
| curl_setopt($curl,CURLOPT_FOLLOWLOCATION, TRUE);// Locationヘッダを追跡 | |
| curl_setopt($curl, CURLOPT_CAINFO, 'cacert.pem'); | |
| curl_setopt($curl, CURLOPT_HTTPHEADER, [ | |
| "Authorization: ".$token, | |
| ]); | |
| if($request !== false) curl_setopt($curl,CURLOPT_CUSTOMREQUEST,$request); | |
| if($data !== false){ | |
| curl_setopt($curl,CURLOPT_POST, TRUE); | |
| curl_setopt($curl, CURLOPT_POSTFIELDS,json_encode($data)); | |
| } | |
| curl_setopt($curl,CURLOPT_USERAGENT, "USER_AGENT"); | |
| curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
| $result = curl_exec($curl); | |
| $errno = curl_errno($curl); | |
| $error = curl_error($curl); | |
| $statuscode = curl_getinfo($curl, CURLINFO_RESPONSE_CODE); | |
| curl_close($curl); | |
| if($errno !== CURLE_OK){ | |
| if($errno === 3){ | |
| //https://stackoverflow.com/questions/35913519/php-curl-return-curl-errno-3 | |
| //https://curl.se/libcurl/c/libcurl-errors.html | |
| throw new \RuntimeException('CURLE_URL_MALFORMAT(3): The URL was not properly formatted. url: "'.$url.'"', $errno); | |
| } | |
| throw new \RuntimeException($error, $errno); | |
| } | |
| //var_dump("StatusCode: ".$statuscode); | |
| $test = json_decode($result,true); | |
| //var_dump($data); | |
| //var_dump($test); | |
| sleep(1); | |
| if($statuscode >= 400){ | |
| var_dump("StatusCode: ".$statuscode); | |
| throw new \RuntimeException("request failed: received status code \"".$statuscode."\""); | |
| } | |
| return $test; | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment