Posted at

basic認証ありのサイトを偽装user agentでfile_get_contentsする

headerにbasic認証用のAuthorizationと、User-Agentを追加するだけ(´▽`)

    $opts = array(

'http' => [
'method' => 'GET',
'header' => implode("\r\n",[
'Authorization: Basic ' . base64_encode('ID:PASS'),
'User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 12_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Mobile/15E148 Safari/604.1',
]),
],
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false
]
);

file_get_contents($url, false, stream_context_create($opts));