PHP+Curl远程模拟代理IP登录并获取数据
效果代码:
<?php
$post = array(
'username' => '帐号',
'password' => '密码',
'bind' => '',
);
$url = "http://127.0.0.1/login.html"; //登录地址
$cookie = dirname(__FILE__) . '/cookie_oschina.txt'; //设置cookie保存路径
$url2 = "http://127.0.0.1/index.html"; //登录后要获取信息的地址
login_post($url, $cookie, $post); //模拟登录
$content = get_content($url2, $cookie); //获取$url2的信息
@ unlink($cookie); //删除cookie文件
$preg = '/<list class="list-group-item"> 平台金额: (.*) /i'; //匹配页面信息
preg_match_all($preg, $content, $arr);
$pregs = '/<list class="list-group-item"> 网站帐号: (.*)/i'; //匹配页面信息
preg_match_all($pregs, $content, $arrs);
echo "开源中国昵称:". $arr[1][0];//输出内容
echo "网站帐号:". $arrs[1][0];//输出内容
function login_post($url, $cookie, $post) {
$ip = "175.42.123.178";
$dk = 9999;
$curl = curl_init(); //初始化curl模块
curl_setopt($curl, CURLOPT_URL, $url); //登录提交的地址
curl_setopt($curl, CURLOPT_HEADER, 0); //是否显示头信息
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //是否自动显示返回的信息 0 有跳转 1直接到登录后页面
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie); //设置Cookie信息保存在指定的文件中
curl_setopt($curl, CURLOPT_POST, 1); //post方式提交
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); //代理认证模式
curl_setopt($curl, CURLOPT_PROXY, $ip); //代理服务器地址
curl_setopt($curl, CURLOPT_PROXYPORT, $dk); //代理服务器端口
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post)); //要提交的信息
curl_exec($curl); //执行cURL
curl_close($curl); //关闭cURL资源,并且释放系统资源
}
function get_content($url, $cookie) {
$ip = "175.42.123.178";
$dk = 9999;
$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_COOKIEFILE, $cookie); //读取cookie
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); //代理认证模式
curl_setopt($ch, CURLOPT_PROXY, $ip); //代理服务器地址
curl_setopt($ch, CURLOPT_PROXYPORT, $dk); //代理服务器端口
$rs = curl_exec($ch); //执行cURL抓取页面内容
curl_close($ch);
return $rs;
}
?>
黑蜘蛛