php post模拟登陆,php通过curl函数取得数据、模拟登陆、POST数据

费话不说多在php中我们如果要取得数据、模拟登陆、POST数据等功能第一个想到的肯定是curl函数了,这个函数方便实用并且还可以多线程了下面整理了一个例子,有兴趣的朋友可参考。

例子,使用php curl获取网页数据的方法:<?php

$ch = curl_init();

//设置选项,包括URL

curl_setopt($ch, CURLOPT_URL, "http://www.phprm.com");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_HEADER, 0);

//执行并获取HTML文档内容

$output = curl_exec($ch);

//释放curl句柄

curl_close($ch);

?>

使用php curl post提交数据的方法<?php

$url = "http://www.phprm.com/ curl_post.php";

$post_data = array(

"nameuser" => "syxrrrr",

"pw" => "123456"

);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

$output = curl_exec($ch);

curl_close($ch);

echo $output;

?>

取得数据、模拟登陆、POST数据<?php

/********************** curl 系列 ***********************/

//直接通过curl方式取得数据(包含POST、HEADER等)

/*

* $url: 如果非数组,则为http;如是数组,则为https

* $header: 头文件

* $post: post方式提交 array形式

* $cookies: 0默认无cookie,1为设置,2为获取

*/

public function curl_allinfo($urls, $header = FALSE, $post = FALSE, $cookies = 0) {

$url = is_array($urls) ? $urls['0'] : $urls;

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//带header方式提交

if ($header != FALSE) {

curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

}

//post提交方式

if ($post != FALSE) {

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

}

if ($cookies == 1) {

curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile");

} else if ($cookies == 2) {

curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile");

}

if (is_array($urls)) {

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

}

$data = curl_exec($ch);

curl_close($ch);

return $data;

}

?>

最后附一个模仿搜索引擎蜘蛛来抓取网页<?php

function get_web_page($url) {

$options = array(

CURLOPT_RETURNTRANSFER => true, // return web page 返回网页

CURLOPT_HEADER => false, // 不返回头信息

CURLOPT_FOLLOWLOCATION => true, // follow redirects

CURLOPT_ENCODING => "", // handle all encodings

CURLOPT_USERAGENT => "spider", // 设置UserAgent

CURLOPT_AUTOREFERER => true, // set referer on redirect

CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect 连接超时

CURLOPT_TIMEOUT => 120, // timeout on response 回复超时

CURLOPT_MAXREDIRS => 10, // stop after 10 redirects

);

$ch = curl_init($url);

curl_setopt_array($ch, $options);

$content = curl_exec($ch);

$err = curl_errno($ch);

$errmsg = curl_error($ch);

$header = curl_getinfo($ch);

curl_close($ch);

$header[''errno''] = $err;

$header[''errmsg''] = $errmsg;

$header[''content''] = $content;

return $header;

}

?>

转载随意,但请附上文章地址:-)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值