<?php
function HttpsPost($url, $data = []) {
$ch = curl_init();
//设置选项,包括 URL
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //对证书来源的检查
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5"); //模拟用户使用的浏览器
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //使用自动跳转
curl_setopt($ch, CURLOPT_AUTOREFERER, 1); //自动设置 Referer
curl_setopt($ch, CURLOPT_POST, 1); //发送一个 常规的 Post 请求
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); //要提交的信息
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置超时限制防止死循环
curl_setopt($ch, CURLOPT_HEADER, 0); //显示返回的 Header 区域内容
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //获取的信息以文件流的形式返回
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . '/curl.cookie');
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/curl.cookie');
$output = curl_exec($ch); //执行操作
if (curl_errno($ch)) {
return "Errno" . curl_error($ch); //捕抓异常
}
curl_close($ch); //关闭 CURL
return $output;
}
//获取登录所需参数
$loginFormUrl = '
https://www.appannie.com/account/login';//设置 cookie 保存路径
$loginForm = HttpsPost($loginFormUrl, []);
$preg_csrfmiddlewaretoken = "/<input\s.*?name=\'csrfmiddlewaretoken\'.*?value=\'(.*?)\'.*?\/>/";
$preg_next = "/<input\s.*?name=\"next\".*?value=\"(.*?)\".*?\/>/";
preg_match($preg_csrfmiddlewaretoken, $loginForm, $match_csrfmiddlewaretoken);
preg_match($preg_next, $loginForm, $match_next);
//设置 post 的数据
$post['csrfmiddlewaretoken'] = $match_csrfmiddlewaretoken[1];
$post['next'] = isset($match_next[1]) ? $match_next[1] : '/apps/ios/top/china/games/';
$post['username'] = '***@***.com';
$post['password'] = '******';
$post['remember_user'] = 'on';
//提交登录
$loginPost = HttpsPost($loginFormUrl, $post);//未登录成功
//获取信息
$url2 = '
http://www.appannie.com/apps/ios/top/china/games/?device=iphone';$iphone_china = HttpsPost($loginFormUrl);
echo $iphone_china;