Memo

メモ > 技術 > プログラミング言語: PHP > file_get_contentsでリクエストする

■file_get_contentsでリクエストする
HTTPメソッド(CRUD)についてまとめた - Qiita https://qiita.com/Ryutaro/items/a9e8d18467fe3e04068e PHP(GET、POST、PUT、またはDELETE)で要求タイプを検出する [request] | CODE Q&A 問題解決 [日本語] https://code.i-harness.com/ja/q/57a87 PHP の file_get_contents は get どころか post も put も delete も upload もできる - tototoshi の日記 http://tototoshi.hatenablog.com/entry/2014/06/10/011223 LaravelにフォームからPUT/DELETEリクエストを送る - Qiita https://qiita.com/ozhaan/items/c1e394226c1d5acb7f0e ■基本的なリクエスト file_get_contents と file_put_contents を使うと、 ローカルファイルの読み書きだけでなくHTTPリクエストできる 失敗すると false が返ってくる 空文字が返ってきた場合と区別するために、「===」で比較する必要があるので注意
<?php $result = file_get_contents('http://localhost/~test/request/target.php'); if ($result === false) { echo 'NG'; } else { echo 'OK'; }
file_put_contents で保存する場合も失敗すると false が返ってくる 0byteの文字を書き込んだ場合と区別するために、「===」で比較する必要があるので注意
<?php if (file_put_contents('http://localhost/~test/request/target.php', $message) === false) { echo 'NG'; } else { echo 'OK'; }
■色々なリクエスト(リクエストする側)
<?php // 単純なGET echo file_get_contents('http://localhost/~test/request/target.php'); echo '<hr>'; // ユーザーエージェントを指定してGET echo file_get_contents( 'http://localhost/~test/request/target.php?test=ABC', false, stream_context_create( array( 'http' => array( 'method' => 'GET', 'header' => 'Content-Type: text/html' . "\r\n" . 'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)', ) ) ) ); // GET echo file_get_contents( 'http://localhost/~test/request/target.php?test=GETのテスト', false, stream_context_create( array( 'http' => array( 'method' => 'GET', 'header' => 'Content-Type: text/html', ) ) ) ); echo '<hr>'; // POST echo file_get_contents( 'http://localhost/~test/request/target.php', false, stream_context_create( array( 'http' => array( 'method' => 'POST', 'header' => 'Content-Type: application/x-www-form-urlencoded', 'content' => http_build_query( array( 'test' => 'POSTのテスト', ) ) ) ) ) ); echo '<hr>'; // PUT echo file_get_contents( 'http://localhost/~test/request/target.php', false, stream_context_create( array( 'http' => array( 'method' => 'PUT', 'header' => 'Content-Type: text/html', 'content' => http_build_query( array( 'test' => 'PUTのテスト', ) ) ) ) ) ); echo '<hr>'; // DELETE echo file_get_contents( 'http://localhost/~test/request/target.php', false, stream_context_create( array( 'http' => array( 'method' => 'DELETE', 'header' => 'Content-Type: text/html', ) ) ) ); echo '<hr>';
上の例でリクエストされるファイルの内容は以下のとおり
<?php switch ($_SERVER['REQUEST_METHOD']) { case 'GET': echo 'GET:'; break; case 'POST': echo 'POST:'; break; case 'PUT': echo 'PUT:'; break; case 'DELETE': echo 'DELETE:'; break; case 'HEAD': echo 'HEAD:'; break; case 'OPTIONS': echo 'OPTIONS:'; break; case 'TRACE': echo 'TRACE:'; break; case 'CONNECT': echo 'CONNECT:'; break; default: echo 'NG'; break; } print_r($_SERVER); print_r($_REQUEST); parse_str(file_get_contents('php://input'), $parameter); print_r($parameter);
■SSLにリクエスト(認証エラーになる場合) 無効な証明書なサイトに、file_get_contents する方法 - Qiita https://qiita.com/izanari/items/f4f96e11a2b01af72846
$context = [ 'ssl' => [ 'verify_peer' => false, 'verify_peer_name' => false, ], ]; $result = file_get_contents('https://localhost/~test/request/target.php', false, stream_context_create($context));
■JSONでリクエスト
$context = [ 'http' => [ 'method' => 'POST', 'header' => 'Content-type: application/json; charset=UTF-8', 'content' => json_encode([ 'param1' => 'aaa', 'param2' => 'bbb', 'param3' => 'ccc', ]), ], 'ssl' => [ 'verify_peer' => false, 'verify_peer_name' => false, ], ]; $result = file_get_contents('https://localhost/~test/request/target.php', false, stream_context_create($context));

Advertisement