Home > Backend Development > PHP Tutorial > php的curl抓包

php的curl抓包

WBOY
Release: 2016-06-23 13:29:46
Original
1819 people have browsed it

在PHP中实现抓包有两种方式,一个是使用file_get_contents()函数采集页面内容,另一种就是curl

CURL请求过程

curl完成请求主要是分为以下四步:
1、初始化,创建一个新的curl资源(即:curl_init())
2、设置URL和相应的选项(即:curl_setopt() )
3、抓取URL并把它传递给浏览器(即:curl_exec())
4、关闭curl资源,并且释放系统资源(即:curl_close())

CURL提交get请求

curl抓包最简单的一个例子,抓取页面内容(按着上面的四个步骤):        $init = curl_init();        $url = "http://xiongchao.net.cn/";        curl_setopt($init,CURLOPT_URL,$url);        $res = curl_exec($init);        curl_close($init);        echo $res;注:第二步只最关键的一部分,在这里可以设置一些高级选项,例如:CURLOPT_HEADER,CURLOPT_POST等选项curl_setopt()的参数介绍:  [http://php.net/manual/zh/function.curl-setopt.php](http://php.net/manual/zh/function.curl-setopt.php)
Copy after login

CURL提交post请求

<html>    <head>        <meta charset=gbk>    </head>    <body>        <form action="" method="post">            账号:<input type="text" name="user">        <br>            密码:<input type="password" name="password">        <br>        <input type="submit" name="login" value="登录">        </form>    </body><?php if($_POST){ $login = $_POST["user"]; $password = $_POST["password"]; $url = "http://xiongchao.net.cn/admin.php/Login/do_login"; $fields = "login_name=".$login."&password=".$password; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_HEADER,0);//设置header 1:输出header信息 0:不输出header信息 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//要求结果为字符串且输出到屏幕上 curl_setopt($ch,CURLOPT_POST,1); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); $result = curl_exec($ch); curl_close($ch); echo $result; } ?></html>
Copy after login

请求的结果如下图:

版权声明:本文为博主原创文章,未经博主允许不得转载。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template