分享

有朋友可以帮忙用PHP翻译一段PYTHON代码吗?

 quasiceo 2015-03-09
By lusin at 2013-12-17 01:00:13 +08:00, 1729 次点击
http://www.oschina.net/code/snippet_251926_23749
中的python模拟登陆阿里妈妈的代码,哪位朋友可以帮翻译为php或者指出我翻译的哪儿有问题吗?

我翻译成(不过无法登陆):
<?php
function g($url){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_USERAGENT,$_SERVER ['HTTP_USER_AGENT'] );
//curl_setopt($ch,CURLOPT_HTTPHEADER, $header);
$content=curl_exec($ch);
$pattern="/input name='_tb_token_' type='hidden' value='([a-zA-Z0-9]+)'/";
$aaa=preg_match($pattern,$content,$v);
return trim($v[1]);
}
$url='https://www.alimama.com/member/minilogin_act.htm';
$ref='http://www.alimama.com/member/minilogin.htm';
$data['logname']='';
$data['originalLogpasswd']='';
$data['logpasswd']='';
$data['style']='';
$data['redirect']='';
$data['proxy']='';
$data['_tb_token_']=g($ref);
$fields=http_build_query($data);
$cookie_file="d:\cookie.txt";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_USERAGENT,$_SERVER ['HTTP_USER_AGENT'] );
curl_setopt($ch,CURLOPT_REFERER,$ref);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields);
curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie_file);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);
curl_setopt($ch,CURLOPT_HEADER,0);
$str=curl_exec($ch);
curl_close($ch);
8 回复  |  直到 2013-12-17 11:14:47 +08:00

    1
niux2   2013-12-17 01:15:00 +08:00 via Android
同求。研究这个好久了。py源代码能过,php过不去。

    2
txlty   2013-12-17 03:01:36 +08:00   2
不会python,不过可以帮你写一个php的。你没理解_tb_token_是怎么验证的,那个正则好像也错了。处理https的方式也不对。
<?php
$ua=$_SERVER['HTTP_USER_AGENT'];
$cookie_file = 'cookie.txt';
$username='xxxxxx@126.com';
$password='password';

$ch = curl_init('http://www.alimama.com/member/minilogin.htm');
curl_setopt($ch, CURLOPT_USERAGENT, $ua);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
$contents=curl_exec($ch);
curl_close($ch);
$arr=explode("<input name='_tb_token_' type='hidden' value='",$contents);
$arr1=explode("'>",$arr[1]);
$token=$arr1[0]; //不会写正则。so 拆数组。

$data['logname']=$username;
$data['originalLogpasswd']=$password;
$data['logpasswd']=md5($data['originalLogpasswd']);
$data['style']='';
$data['redirect']='';
$data['proxy']='';
$data['_tb_token_']=$token;
$post_fields=http_build_query($data);
$ch = curl_init('https://www.alimama.com/member/minilogin_act.htm');
curl_setopt($ch, CURLOPT_USERAGENT, $ua);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$contents=curl_exec($ch);
curl_close($ch);//已经登上了,带上cookie即可访问登录后页面

//输出登录后的阿里妈妈首页
$ch = curl_init('http://www.alimama.com/index.htm');
curl_setopt($ch, CURLOPT_USERAGENT, $ua);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
$contents=curl_exec($ch);
curl_close($ch);
echo $contents;
?>

    3
lusin   2013-12-17 09:57:34 +08:00
@txlty 非常感谢

    4
imcj   2013-12-17 10:51:52 +08:00
给出报价是最靠谱的做法。

    5
lusin   2013-12-17 10:54:25 +08:00
@txlty 请问你那边测试在阿里妈妈新改版后能获取到除“http://www.alimama.com/index.htm”页面外的内容吗?比如:“http://pub.alimama.com/index.htm?spm=0.0.0.0.YhtHLr#!/promo/self/items”

    6
lusin   2013-12-17 10:59:12 +08:00
@imcj 这个主要是以学习为主,自己费了半天劲搞不出来,想知道下原因。
买来这个东西也快不能用了,阿里妈妈最近会全部统一为用淘宝账号登陆

    7
imcj   2013-12-17 11:06:17 +08:00
@lusin 你要认为别人应该为免费你调试程序,这我也没什么好说的了。

    8
lusin   2013-12-17 11:14:47 +08:00
@imcj 不知如何说这个问题,我从来没认为人们应该要做什么,你非要这样理解,我也没什么好说的。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约