- //下面是測試用的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>";
- }