LoginSignup
20
18

More than 5 years have passed since last update.

PHPで、サイトをHTMLソースとして丸ごと保存する方法

Last updated at Posted at 2014-10-11

ここでは、
・バーチャルドメイン: blog.example.com
・Basic認証をかけている
・PC/スマホページを同じURLで表示するサイトにおいて、スマホのページのHTMLを取得する
という前提条件で記述しています。

// Virtual HOST名
$virtual_host_name = "blog.example.com";

// User Agent (スマホのデバイスとして変更・・・スマホのUser Agentであれば、何でも良いです。)
$user_agent = "Mozilla/5.0 (Linux; Android 4.4.2; SOL23 Build/14.3.C.0.239) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.114 Mobile Safari/537.36";

$options = array(
    'http' => array(
        "method" => "GET",
        "header" => "Host: {$virtual_host_name}\r\n" .
                     "User-Agent: {$user_agent}",
    ),
);
$context = stream_context_create($options);

// HOST名
$host = "127.0.0.1";

// Basic認証を使用している場合、IDとパスワード+@マーク
$basic_auth = "yamada:xxxxx@";

// HTML取得
$target_url = "http://{$basic_auth}{$host}";
$site_html = file_get_contents( $target_url, false, $context);
20
18
1

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
20
18