zoukankan      html  css  js  c++  java
  • PHP中使用CURL(六)

    curl常用的几个例子

    1、抓取无访问控制文件

    1 <?php
    2  $ch = curl_init();
    3  curl_setopt($ch, CURLOPT_URL, "http://localhost/mytest/phpinfo.php");
    4  curl_setopt($ch, CURLOPT_HEADER, false);
    5  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //如果把这行注释掉的话,就会直接输出
    6  $result=curl_exec($ch);
    7  curl_close($ch);
    8  ?>

    2、使用代理进行抓取

    <?php
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, "http://blog.snsgou.com");
     curl_setopt($ch, CURLOPT_HEADER, false);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    &nbsp;curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
    &nbsp;curl_setopt($ch, CURLOPT_PROXY, 125.21.23.6:8080);
    &nbsp;//url_setopt($ch, CURLOPT_PROXYUSERPWD, 'user:password');如果要密码的话,加上这个
     $result=curl_exec($ch);
     curl_close($ch);
     ?>

    3、post数据后,抓取数据

    <?php
    $ch = curl_init();
    $data = array('name' => 'test', 'sex' => 1);//array(1)
    curl_setopt($ch, CURLOPT_URL, 'http://localhost/mytest/curl/userinfo.php');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_exec($ch);
    ?>

    4、抓取一些有页面访问控制的页面

    <?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://club-china");
    curl_setopt($ch, CURLOPT_USERPWD, '[username]:[password]');//破解页面访问控制
    curl_setopt($ch, CURLOPT_HTTPGET, 1);
    curl_setopt($ch, CURLOPT_REFERER, "http://club-china");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $result = curl_exec($ch);
    curl_close($ch);
    ?>

    5、模拟登录

    <?php
    function checkLogin($user, $password){
        if (empty($user) || empty($password)){
            return false;
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_REFERER, "http://mail.sina.com.cn/index.html");
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_USERAGENT, USERAGENT);
        curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);
        curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
        curl_setopt($ch, CURLOPT_URL, "http://mail.sina.com.cn/cgi-bin/login.cgi");
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "&logintype=uid&u=" . urlencode($user) . "&psw=" . $password);
        $contents = curl_exec($ch);
        curl_close($ch);
        if (!preg_match("/Location: (.*)\/cgi\/index\.php\?check_time=(.*)
    /", $contents, $matches)){
            return false;
        }else{
            return true;
        }
    }
    define("USERAGENT", $_SERVER['HTTP_USER_AGENT']);
    define("COOKIEJAR", tempnam("c:windwos	emp", "cookie"));
    define("TIMEOUT", 500);
    echo checkLogin("username", "password");
    ?>

    6、文件上传

    <?php
    /**
     * @param string $target_url 上传目标地址
     * @param string $filename 上传文件路径
     * @param string $form_name 表单名称
     */
    function curlUploadFile($target_url, $filename, $form_name) {
        $upload_file = new CURLFile($filename);
        $post_data = array(
            $form_name => $upload_file
        );
    
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $target_url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
        $result = curl_exec($ch);
        curl_close($ch);
    }
    
    $target_url = 'http://www.codean.net/notFound/test.php';
    $filename = realpath("C:/Users/HelloWorld/Desktop/Images/1.jpg");
    $form_name = 'file';
    
    // 接收端使用$_FILES接受
    curlUploadFile($target_url, $filename, $form_name);
    ?>

    7、文件流上传

    /*
     * 第三种写法,使用PHP流发送
     * @param string $target_url 上传目标地址
     */
    function curlUploadFile($target_url) {
        $fh = fopen('php://temp', 'rw+');
        $string = 'Hello World';
        fwrite($fh, $string);
        rewind($fh);
    
        $ch =  curl_init();
        curl_setopt($ch, CURLOPT_URL, $target_url);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
        curl_setopt($ch, CURLOPT_PUT, true);
        curl_setopt($ch, CURLOPT_INFILE, $fh);
        curl_setopt($ch, CURLOPT_INFILESIZE, strlen($string));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
        $result = curl_exec($ch);
        curl_close($ch);
    }
    $target_url = 'http://www.codean.net/notFound/test.php';
    curlUploadFile($target_url);
    
    // 接收端取出流文件并保存
    $putdata = fopen('php://input', 'r');
    $fp = fopen('test.txt', 'w');
    while ($data = fread($putdata, 1024)) {
        fwrite($fp, $data);
    }
    fclose($fp);
    fclose($putdata);
  • 相关阅读:
    php中常用的字符串函数,大全了
    php写的验证码实现方法
    简单的php做的一个验证码生成方案
    ActiveMQ+SpringMVC+Maven应用示例
    zookeeper简单操作
    zookeeper基本概念及原理
    zookeeper 事务日志与快照日志
    activeMQ安装与测试
    activeMQ "HelloWorld"实现
    JMS基本概念
  • 原文地址:https://www.cnblogs.com/yudis/p/5609033.html
Copyright © 2011-2022 走看看