zoukankan      html  css  js  c++  java
  • php中能够获取到某一网站内容的方法

    方法一:file_get_contents 函数

    example:

    <?php
    $url = "http://www.cnblogs.com";
    $contents = file_get_contents($url);
    echo $contents;
    ?> 

    出现乱码需要在输出前加一句:

    $getcontent = iconv("gb2312", "utf-8",$contents); 

    方法二:fopen

    example:

    <?php
    $handle = fopen ("http://www.cnblogs.com", "rb");
    $contents = "";
    do {
    $data = fread($handle, 1024);
    if (strlen($data) == 0) {
    break;
    }
    $contents .= $data;
    } while(true);
    fclose ($handle);
    echo $contents;
    ?> 

    但是方法一和方法二需要服务器中php的配置开启了“allow_url_fopen = On”,才允许远端访问

     方法三:curl(这种方法比较好用。)

    <?php
    $url = "http://www.cnblogs.com";
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    //在需要用户检测的网页里需要增加下面两行
    //curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    //curl_setopt($ch, CURLOPT_USERPWD, US_NAME.":".US_PWD);
    $contents = curl_exec($ch);
    curl_close($ch);
    echo $contents;
    ?> 
    
  • 相关阅读:
    Python的运算符
    RabbitMQ 的配置文件
    安装新版本的rabbitmq
    Ubuntu 16.04 安装rabbitmq
    Python Web 版本tailf, grep
    解决pycharm问题:module 'pip' has no attribute 'main'
    Python argparse
    Ansible 并行和异步
    cef相关
    浏览器透明设置例子,qt5.6才支持
  • 原文地址:https://www.cnblogs.com/share123/p/3532443.html
Copyright © 2011-2022 走看看