stream_context_create と組み合わせて使います。
手元でてきとーに動かしてた REST API とかで試してます。
get
普通ですね。
<?php $content = json_decode(file_get_contents("http://localhost:5000/api/note/161"));
post
<?php $context = stream_context_create( array( 'http' => array( 'method'=> 'POST', 'header'=> 'Content-type: application/json; charset=UTF-8', 'content' => json_encode( array( 'title' => 'file_get_contents で POST', 'raw' => "file_get_contents で POST\nPHP すごい...\n" ) ) ) ) ); file_get_contents('http://localhost:5000/api/note', false, $context);
なるほど〜。file_get_contents は file_post_contents だったのか〜。
put
<?php $context = stream_context_create( array( 'http' => array( 'method'=> 'PUT', 'header'=> 'Content-type: application/json; charset=UTF-8', 'content' => json_encode( array( 'id' => 162, 'title' => 'file_get_contents で PUT', 'raw' => "file_get_contents で PUT\nPHP すごい...\n" ) ) ) ) ); file_get_contents('http://localhost:5000/api/note/162', false, $context);
なるほど〜。file_get_contents は file_put_contents だったのか〜。
delete
<?php $context = stream_context_create( array( 'http' => array( 'method'=> 'DELETE' ) ) ); file_get_contents('http://localhost:5000/api/note/162', false, $context);
なるほど〜。file_get_contents は file_delete_contents だったのか〜。
upload
<?php $upload_content = file_get_contents('upfile.txt'); $context = stream_context_create( array( 'http' => array( 'method'=> 'POST', 'header'=> 'Content-Type: multipart/form-data; boundary=-PHP_FILE_GET_CONTENTS', 'content' => "---PHP_FILE_GET_CONTENTS Content-Disposition: form-data; name=\"upfile\"; filename=\"upfile.txt\" Content-Type: text/plain {$upload_content} ---PHP_FILE_GET_CONTENTS " ) ) ); file_get_contents("http://localhost:5001/test_upload.php", false, $context);
なるほど〜。file_get_contents は file_upload_contents だったのか〜。
ひとこと
無理すんなや。