curl 模拟登陆
//登录用的curl
$curlPost = 'username=admin&password=123456'; 数据
$tmp = tempnam('./tmp', 'login'); //存储的临时地址
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, 'http://abby.study.net/?do=login'); url地址
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); // 数据
curl_setopt($ch, CURLOPT_COOKIEJAR, $tmp); //地址
$data = curl_exec($ch);
curl_close($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, 'http://abby.study.net/?do=login'); url地址
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); // 数据
curl_setopt($ch, CURLOPT_COOKIEJAR, $tmp); //地址
$data = curl_exec($ch);
curl_close($ch);
//访问需要登录后才能访问的页面
$ch = curl_init('http://abby.study.net/?mod=read'); //文件
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, $tmp); //模拟登录创建的地址文件
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $curlPost ); //参数
$content = curl_exec($ch);
curl_close($ch);
$content = curl_exec($ch);
curl_close($ch);
(3)上传文件
$file
= array(
"video" => "@F:/test.mp4"
); //文件路径,前面要加@,表明是文件上传.
$curl = curl_init("http://abby.study.net/upload.php");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_exec($curl);
curl_close($curl);
"video" => "@F:/test.mp4"
); //文件路径,前面要加@,表明是文件上传.
$curl = curl_init("http://abby.study.net/upload.php");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_exec($curl);
curl_close($curl);
(4)下载文件
$ch = curl_init('http://abby.study.net/test.flv');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
$data = curl_exec($ch);
if (curl_error()) {
echo curl_error();
} else {
if (file_put_contents('./test.flv', $data)) {
echo 'success!';
} else {
echo 'failure!';
}
}
curl_close($ch);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
$data = curl_exec($ch);
if (curl_error()) {
echo curl_error();
} else {
if (file_put_contents('./test.flv', $data)) {
echo 'success!';
} else {
echo 'failure!';
}
}
curl_close($ch);
该文章学习于 http://blog.sina.com.cn/s/blog_6f7ef65401018npu.html