LoginSignup
13
14

More than 5 years have passed since last update.

PHPでWebスクレイピングするときのエージェント設定

Last updated at Posted at 2016-07-10

最近PHPでWebスクレイピングすることが多くなって少し困ったことをがあったので解決法も書きます

<?php
$url='http://example.com // 参照URL
$websc = file_get_contents($url.$input_data);
?>

大概のサイトはこれだけでソースは持ってこれるのですがまれにエージェントがないとダメなサイトがあったのでゴニョゴニョいじってたら下記の感じで行けました。

<?php
$url='http://example.com // 参照URL
$context = stream_context_create(array(
'http' => array(
'method' => 'GET',
'header' => 'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)',
)));
$websc = file_get_contents($url.$input_data,false,$context);
?>

ただ単にcontextにエージェント情報を入れただけですね
これだけで出来たので何かの役に立てばと

では

13
14
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
14