duandu8202 2015-07-28 21:33 采纳率: 0%
浏览 49

php获取文件内容浏览器

I get page contents with this php code: but I don't know why server access this page with old browser version

$url = $target_domain . 'http://www.facebook.com';
//Download page
$site = file_get_contents($url);
$dom = DOMDocument::loadHTML($site);

if($dom instanceof DOMDocument) {
    // find <head> tag
    $head_tag_list = $dom->getElementsByTagName('head');
    // there should only be one <head> tag
    if($head_tag_list->length !== 1) {
        throw new Exception('Wow! The HTML is malformed without single head tag.');
    }
    $head_tag = $head_tag_list->item(0);

    // find first child of head tag to later use in insertion
    $head_has_children = $head_tag->hasChildNodes();
    if($head_has_children) {
        $head_tag_first_child = $head_tag->firstChild;
    }

    // create new <base> tag
    $base_element = $dom->createElement('base');
    $base_element->setAttribute('href', $target_domain);

    // insert new base tag as first child to head tag
    if($head_has_children) {
        $base_node = $head_tag->insertBefore($base_element, $head_tag_first_child);
    } else {
        $base_node = $head_tag->appendChild($base_element);
    }

    echo $dom->saveHTML();
} else {
    // something went wrong in loading HTML to DOM Document
    // provide error messaging
}
?>

take a look on photo : enter image description here";

please help me how to access with new version of browsers.

  • 写回答

1条回答 默认 最新

  • doudou5421 2015-07-28 23:03
    关注

    Instead of using file_get_contents which is little poor, you can use the Curl which we can add useful parameters, in your case you should indicate a new UserAgent in the request, try this this simple exemple :

    function curl_download($Url) {
    
        // is cURL installed yet?
        if (!function_exists('curl_init')) {
            die('Sorry cURL is not installed!');
        }
    
        // OK cool - then let's create a new cURL resource handle
        $ch = curl_init();
    
        // Now set some options (most are optional)
        // Set URL to download
        curl_setopt($ch, CURLOPT_URL, $Url);
    
    
        // User agent
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36");
    
    
    
    
        // Include header in result? (0 = yes, 1 = no)
        curl_setopt($ch, CURLOPT_HEADER, 0);
    
        // Should cURL return or print out the data? (true = return, false = print)
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    
        // Timeout in seconds
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
    
        // Download the given URL, and return output
        $output = curl_exec($ch);
    
        // Close the cURL resource, and free system resources
        curl_close($ch);
    
        return $output;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c