「file_get_contentsでhttp、https通信する」
file_get_contentsでhttp、httpsで通信するには。
・GET、POST
・基本認証
・クライアント証明書
回答
前提条件
1.http,https通信する場合はphp.iniでallow_url_fopenを有効にする必要があります。
allow_url_fopen=on
php.iniを変更できない場合phpファイル内で有効にする必要があります。
ini_set("allow_url_fopen",1);
2.https通信する場合はphp.iniでopensslを有効にする必要があります。
extension=php_openssl.dll
ソース
●GETの場合
//php.iniを変更できない場合phpファイル内で有効にする
ini_set("allow_url_fopen",1);
$options=[
'http'=>[
'method' => 'GET',
'timeout'=>10, //タイムアウト秒
'ignore_errors' => true, //ステータスコードが4xxや5xxなど失敗の場合でもコンテンツを取得
]
];
$url='https://localhost/';
$context=stream_context_create($options);
$res=file_get_contents($url,false,$context);
//レスポンスヘッダーの1行目からhttpレスポンスコードを取得
preg_match('/^HTTP\/1\.[0|1|x] ([0-9]+) (.*)$/',$http_response_header[0],$matches);
$http_response_code=$matches[1]; //httpレスポンスコード
$http_response_text=$matches[2]; //httpレスポンステキスト
if($res){echo $res;}else{echo "error\n";}
●POSTの場合
//php.iniを変更できない場合phpファイル内で有効にする
ini_set("allow_url_fopen",1);
//POSTするデータを設定
$data=['a'=>'あいうえお','b'=>'1'];
//POST先に合わせて文字コードを変換 (必要な場合)
mb_convert_variables('UTF-8', 'UTF-8', $data);
//クエリ文字列の生成
$data = http_build_query($data, "", "&");
$options=[
'http'=>[
'method' => 'POST',
'timeout'=>10, //タイムアウト秒
'header' => 'Content-Type: application/x-www-form-urlencoded'."\r\n".
'Content-Length: '.strlen($data)."\r\n".
'Cache-Control: no-cache',
'content' => $data,
'ignore_errors' => true, //ステータスコードが4xxや5xxなど失敗の場合でもコンテンツを取得
]
];
$url='https://localhost/';
$context=stream_context_create($options);
$res=file_get_contents($url,false,$context);
//レスポンスヘッダーの1行目からhttpレスポンスコードを取得
preg_match('/^HTTP\/1\.[0|1|x] ([0-9]+) (.*)$/',$http_response_header[0],$matches);
$http_response_code=$matches[1]; //httpレスポンスコード
$http_response_text=$matches[2]; //httpレスポンステキスト
if($res){echo $res;}else{echo "error\n";}
●BASIC認証(基本認証)の場合
//php.iniを変更できない場合phpファイル内で有効にする
ini_set("allow_url_fopen",1);
$options=[
'http'=>[
'method' => 'GET',
'timeout'=>10, //タイムアウト秒
'header'=>'Authorization: Basic '.base64_encode('ユーザー名'.':'.'パスワード')."\r\n"
'ignore_errors' => true, //ステータスコードが4xxや5xxなど失敗の場合でもコンテンツを取得
]
];
$url='https://localhost/';
$context=stream_context_create($options);
$res=file_get_contents($url,false,$context);
//レスポンスヘッダーの1行目からhttpレスポンスコードを取得
preg_match('/^HTTP\/1\.[0|1|x] ([0-9]+) (.*)$/',$http_response_header[0],$matches);
$http_response_code=$matches[1]; //httpレスポンスコード
$http_response_text=$matches[2]; //httpレスポンステキスト
if($res){echo $res;}else{echo "error\n";}
●自己証明書など、無効な証明書のサイトにHTTPS通信する場合
//php.iniを変更できない場合phpファイル内で有効にする
ini_set("allow_url_fopen",1);
$options=[
'http'=>[
'method' => 'GET',
'timeout'=>10, //タイムアウト秒
'ignore_errors' => true, //ステータスコードが4xxや5xxなど失敗の場合でもコンテンツを取得
],
'ssl'=>[
'verify_peer'=>false,
'verify_peer_name'=>false,
]
];
$url='https://localhost/';
$context=stream_context_create($options);
$res=file_get_contents($url,false,$context);
//レスポンスヘッダーの1行目からhttpレスポンスコードを取得
preg_match('/^HTTP\/1\.[0|1|x] ([0-9]+) (.*)$/',$http_response_header[0],$matches);
$http_response_code=$matches[1]; //httpレスポンスコード
$http_response_text=$matches[2]; //httpレスポンステキスト
if($res){echo $res;}else{echo "error\n";}
●自己証明書など、無効な証明書のサイトにHTTPS通信する場合でサーバ証明書ファイルがある場合
//php.iniを変更できない場合phpファイル内で有効にする
ini_set("allow_url_fopen",1);
$options=[
'http'=>[
'method' => 'GET',
'timeout'=>10, //タイムアウト秒
'ignore_errors' => true, //ステータスコードが4xxや5xxなど失敗の場合でもコンテンツを取得
],
'ssl'=>[
'verify_peer'=>true,
'cafile'=>'/home/hoge.csr', //サーバ証明書
]
];
$url='https://localhost/';
$context=stream_context_create($options);
$res=file_get_contents($url,false,$context);
//レスポンスヘッダーの1行目からhttpレスポンスコードを取得
preg_match('/^HTTP\/1\.[0|1|x] ([0-9]+) (.*)$/',$http_response_header[0],$matches);
$http_response_code=$matches[1]; //httpレスポンスコード
$http_response_text=$matches[2]; //httpレスポンステキスト
if($res){echo $res;}else{echo "error\n";}
●クライアント証明書が必要な場合
クライアント証明書はpem形式に変換したものを使用する
//php.iniを変更できない場合phpファイル内で有効にする
ini_set("allow_url_fopen",1);
$options=[
'http'=>[
'method' => 'GET',
'timeout'=>10, //タイムアウト秒
'ignore_errors' => true, //ステータスコードが4xxや5xxなど失敗の場合でもコンテンツを取得
],
'ssl'=>[
'verify_peer'=>false,
'verify_peer_name'=>false,
'local_cert', '/home/hoge.pem',//クライアント証明書
'passphrase'=>'パスフレーズ',
]
];
$url='https://localhost/';
$context=stream_context_create($options);
$res=file_get_contents($url,false,$context);
//レスポンスヘッダーの1行目からhttpレスポンスコードを取得
preg_match('/^HTTP\/1\.[0|1|x] ([0-9]+) (.*)$/',$http_response_header[0],$matches);
$http_response_code=$matches[1]; //httpレスポンスコード
$http_response_text=$matches[2]; //httpレスポンステキスト
if($res){echo $res;}else{echo "error\n";}