使用PHP curl模拟浏览器抓取网站信息
官方解释
curl是一个利用URL语法在命令行方式下工作的文件传输工具。curl是一个利用URL语法在命令行方式下工作的文件传输工具。
它支持很多协议:FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE 以及 LDAP。curl同样支持HTTPS认证,HTTP POST方法, HTTP PUT方法, FTP上传, kerberos认证, HTTP上传, 代理服务器, cookies, 用户名/密码认证, 下载文件断点续传,
上载文件断点续传, http代理服务器管道( proxy tunneling), 甚至它还支持IPv6, socks5代理服务器, 通过http代理服务器上传文件到FTP服务器等等,功能十分强大。
curl讲解一
PHP中curl函数应用
简单的来说一共四步
curl_init();
curl_setopt();
curl_exec();
curl_close();
最重要的命令就是 curl_setopt();
一个简单的post请求例子
index.php
复制代码 代码如下:
$url = "http://www.mytest.com/curl/login.php"; //请求的url地址
$user = "zkg111"; //用户名
$pass = "123456";
$postdata = "user_name=".$user."&password=".$pass; //请求的数据,以 & 符号分割
$curl = curl_init(); //开启curl
curl_setopt($curl, CURLOPT_URL, $url); //设置请求地址
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //是否输出 1 or true 是不输出 0 or false输出
curl_setopt($curl, CURLOPT_POST, 1); //是否使用post方法请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata); //post数据
echo $data = curl_exec($curl); //执行curl操作
curl_close($curl);
?>
下面一个简单的例子,我随便打开了兄弟连的论坛,接着就模拟了一下兄弟连论坛的登陆,如果需要发帖的话原理都是一样的,转接页面,提交数据
特别注意的是cookie的保存目录 windows7下面必须是在./temp目录下,开始我自己创建了一个新文件夹,发现存是对的,但是cookie读取的时候不对,为此还在好多地方
提问,但是没有回答对的,折腾了好几天该了保存文件为./temp目录下才可以的,提醒别的朋友别和我一样瞎转
复制代码 代码如下:
$url = "http://bbs.lampbrother.net/login.php";
$urls = "http://bbs.lampbrother.net";
$lgt = 0;
$user = "XXXX";
$pass = "XXXX";
$question = 0;
$hideid = 1;
$cookie_file = tempnam('./temp','cookie');
$postdata = "forward=&jumpurl=".$urls."&step=2&lgt=".$lgt."&pwuser=".$user."&pwpwd=".$pass."&question=".$question."&answer=&hideid=".$hideid;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($ch);
curl_close($ch);
//echo $data;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://bbs.lampbrother.net/');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_exec($ch);
curl_close($ch);
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.
