• PHP curl


    这是PHP的CURL模拟浏览器去发送POST提交,首先我们看下test.php这是用来发送POST提交的程序,代码如下

    <?php
    $remote = "http://localhost/curl/post.php";
    $nameArr = array("a","b","c","d","e");

    for($i=0;$i<10;$i++) {
    $post_data = array (
    "username" => $nameArr[rand(0,5)].rand(0,1000000),
    "email" => rand(100000,20000000)."@qq.com",
    "password" => "111111111",
    "confirm_password" => "1211111111",
    "extend_field2" => "654333311257",
    "extend_field3" => "0551-5588774",
    "extend_field4" => "0551-5588774",
    "extend_field5" => "13865498754",
    "sel_question" => "friend_birthday",
    "passwd_answer" => "1990-01-28",
    "agreement" => 1,
    "act" => "act_register"
    );


    $ch = curl_init($remote);
    //$fp = fopen($local,"w");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    //curl_setopt($cp, CURLOPT_FILE, $fp);
    //curl_setopt($cp, CURLOPT_HEADER, 0);

    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    $out =curl_exec($ch);
    //$info = curl_getinfo($cp);
    //echo "获取". $info["url"] . "耗时". $info["total_time"] . "秒";

    curl_close($ch);
    //fclose($fp);
    print_r($out);
    }
    ?>

    那个 post.php其实什么都没有就是用来接收test.php传递过来的数据的

    <?php
    print_r($_POST);
    echo "<hr>";
    ?>

    大家可以试试看得到什么效果。记得PHP要开启CURL。(PS:今天发现一个问题就是向不是PHP的程序发送数据时,需要http_build_query用这个函数处理下数据,然后就可以正常接收了,我测试的是ASP程序,其他不清楚,估计都是需要的。)

    下面这个是通过CURL来远程下载,直接上代码,自己可以去运行看效果,PHP虽然很多方法可以进行远程下载,但是本人觉得CURL速度最快。

    <?php
    $remote = "http://ptts.iflytek.com/test.wav";
    $local = "C:/a.wav";
    /*$nameArr = array("a","b","c","d","e");

    for($i=0;$i<10;$i++) {
    $post_data = array (
    "username" => $nameArr[rand(0,5)].rand(0,1000000),
    "email" => rand(100000,20000000)."@qq.com",
    "password" => "111111111",
    "confirm_password" => "1211111111",
    "extend_field2" => "654333311257",
    "extend_field3" => "0551-5588774",
    "extend_field4" => "0551-5588774",
    "extend_field5" => "13865498754",
    "sel_question" => "friend_birthday",
    "passwd_answer" => "1990-01-28",
    "agreement" => 1,
    "act" => "act_register"
    );
    */


    $cp = curl_init($remote);
    $fp = fopen($local,"w");
    //curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //curl_setopt($ch, CURLOPT_POST, 1);

    curl_setopt($cp, CURLOPT_FILE, $fp);
    curl_setopt($cp, CURLOPT_HEADER, 0);
    //curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    curl_exec($cp);
    $info = curl_getinfo($cp);
    echo "获取". $info["url"] . "耗时". $info["total_time"] . "秒";
    curl_close($cp);
    //fclose($fp);
    //print_r($out);
    //}

    ?>


    如果是get请求,页面发生跳转的

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);//获取跳转后的页面
    $content =curl_exec($ch);
    curl_close($ch);

    不需要https信任验证 

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);   
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
  • 相关阅读:
    C#随机数的使用
    英才评测 个人性格倾向 IT知识
    有点迷茫
    [转帖]2006年it人士必去的10个网站
    ASP.NET 中 Cookie 的基本知识
    Http请求方法
    Spherical Mercator
    Axure快速原型设计
    【转】Spring Insight 使用介绍
    DB2执行sql文件
  • 原文地址:https://www.cnblogs.com/yimiao/p/2227406.html
Copyright © 2020-2023  润新知