V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  aswid  ›  全部回复第 1 页 / 共 1 页
回复总数  3
<?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;
@zhzhwcn appannie 登录有 cookie 验证,cookie 不正确则无法登录成功,我保存的 cookie 有问题,不知道该怎么做
@jugelizi 保存的 cookie 应该不对所以一直登录失败
cookie:
# Netscape HTTP Cookie File
# http://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

#HttpOnly_www.appannie.com FALSE / TRUE 0 sessionId ".eJxrYKotZNQI5S9OLS7OzM-LT81LTMpJTfFmChVIzEktKolPzkhNzo4vycxNLWRKTkksSQUxueCMQuZQLvYHzIJMvLwsnSLJBZUlVVzxoSHOXIUsmkGFrG1BhWyh3CX5xfGlBSA9KYXsnaV6AAqvJl4:1agNR3:N88XBiDLzuCkRN8ybuY5GhajWnU"
.appannie.com TRUE / FALSE 1458180793 aa_user_token
www.appannie.com FALSE / TRUE 1489630393 csrftoken nnMCiTASiomihPM9xqwgVzJep84jHacI
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1047 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 14ms · UTC 21:51 · PVG 05:51 · LAX 13:51 · JFK 16:51
Developed with CodeLauncher
♥ Do have faith in what you're doing.