- //下面是测试用的URL列表
- $urls = array(
- "http://www.baidu.com",
- "http://www.google.com"
- );
- /*
- * 测试用的浏览器信息
- *
- */
- $browsers = array(
- "standard" => array (
- "user_agent" => "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6 (.NET CLR 3.5.30729)",
- "language" => "en-us,en;q=0.5"
- ),
- "iphone" => array (
- "user_agent" => "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A537a Safari/419.3",
- "language" => "en"
- ),
- "french" => array (
- "user_agent" => "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; GTB6; .NET CLR 2.0.50727)",
- "language" => "fr,fr-FR;q=0.5"
- )
- );
- foreach ($urls as $url) {
- echo "[URL]: $url<br>";
- foreach ($browsers as $test_name => $browser) {
- //初始化curl
- $ch = curl_init();
- // 设置 url
- curl_setopt($ch, CURLOPT_URL, $url);
- // 设置浏览器的特定header
- //CURLOPT_HTTPHEADER: An array of HTTP header fields to set.
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- "User-Agent: {$browser['user_agent']}",
- "Accept-Language: {$browser['language']}"
- ));
- // 页面内容咱们并不须要
- curl_setopt($ch, CURLOPT_NOBODY, 1);
- // 只需返回HTTP header
- curl_setopt($ch, CURLOPT_HEADER, 1);
- // 返回结果,而不是输出它
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- //执行curl操做
- $output = curl_exec($ch);
- curl_close($ch);
- // 有重定向的HTTP头信息吗?
- //preg_match() 返回 pattern 所匹配的次数。
- //要么是 0 次(没有匹配)或 1 次,由于 preg_match()
- // 在第一次匹配以后将中止搜索。preg_match_all() 则相反,会一直搜索到 subject 的结尾处。若是出错 preg_match() 返回 FALSE。
- if (preg_match("!Location: (.*)!", $output, $matches)) {
- echo "$test_name: redirects to $matches[1]\n";
- } else {
- echo "$test_name: no redirection\n";
- }
- }
- echo "<br><br>";
- }