Files
php-question/Question16-Exception/Ex05.php
2026-10-06 12:10:09 +09:00

21 lines
941 B
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* [Ex05 上級 例外処理 - 実在するAPIへの呼び出し]
*
* 難易度:上級
*
* TODO:
* 1. 関数 callAPI($url) を作成してください。
* - $url が有効なURL形式でない場合は、InvalidArgumentException を投げる。
* - $url を用いて、実在するAPI(例:"https://jsonplaceholder.typicode.com/todos/1")への GET リクエストを行う。
* - API呼び出しに失敗した場合は、RuntimeException を投げる。
* - 正常に呼び出せた場合は、APIからのレスポンス(文字列)を返す。
*
* 2. callAPI() を呼び出す際、try-catch-finally ブロックを使用してください。
* - InvalidArgumentException と RuntimeException を個別にキャッチし、エラーメッセージを出力する。
* - finally ブロックで「API call attempt finished.」というメッセージを必ず出力する。
*
*
*/