Last active
January 1, 2016 05:19
-
-
Save mia-0032/8098274 to your computer and use it in GitHub Desktop.
file_get_contentsのちょっとしたオプション
This file contains 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 | |
$url = '適当なURL'; | |
$data = array( | |
'hoge' => 'foo' | |
); | |
//file_get_contentsの設定 | |
$options = array( | |
'http' => array( | |
'method' => 'POST', | |
'ignore_errors' => true, //trueにすると40x,50x系のエラーでも内容を取得できる。 | |
'content' => http_build_query($data), //POSTで送るパラメータはhttp_build_queryで作る。 | |
) | |
); | |
//設定はstream_context_createに配列を渡して作る | |
$contents = file_get_contents($url, false, stream_context_create($options)); | |
echo '-----------ここからコンテンツ--------------<br>'; | |
echo $contents . '<br>'; | |
echo '-----------ここまでコンテンツ--------------<br>'; | |
echo '-----------ここからヘッダー--------------<br>'; | |
//file_get_contentsで発生したHTTPリクエストのヘッダーは$http_response_headerにはいる。 | |
foreach($http_response_header as $header){ | |
echo $header . '<br>'; | |
} | |
echo '-----------ここまでヘッダー--------------<br>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment