PHPのプログラム内からPOSTでリクエストを飛ばしたときに
出たエラーのメモ。
■エラーが出た状態
$url='http://wwww.example.com/xxxx.php';
$data=array(
'data1' => 'xxxxxxxx',
'data2' => 'yyyyyyyy'
);
$options = array(
'http' => array(
'method' => 'POST',
'content' => http_build_query($data)
)
);
$contents = file_get_contents($url, false, stream_context_create($options));
■発生したエラー
Severity: Notice –> file_get_contents() [function.file-get-contents]: Content-type not specified assuming application/x-www-form-urlencoded xxxxxxxxx.php 80
headerを追加して対応。
■修正版
$url='http://wwww.example.com/xxxx.php';
$data=array(
'data1' => 'xxxxxxxx',
'data2' => 'yyyyyyyy'
);
$headers = array(
'Content-Type: application/x-www-form-urlencoded',
'Content-Length: '.strlen(http_build_query($data))
);
$options = array(
'http' => array(
'method' => 'POST',
'content' => http_build_query($data),
'header' => implode("\r\n", $headers),
)
);
$contents = file_get_contents($url, false, stream_context_create($options));
■参考URL
とても簡単にPHPからPOSTリクエストを送信する方法【PHP】
[PHPエラー] Content-type not specified assuming application/x-www-form-urlencoded の対処法