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

2013-12-17 01:00:13 +08:00
 lusin
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);
4116 次点击
所在节点    PHP
8 条回复
niux2
2013-12-17 01:15:00 +08:00
同求。研究这个好久了。py源代码能过,php过不去。
txlty
2013-12-17 03:01:36 +08:00
不会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;
?>
lusin
2013-12-17 09:57:34 +08:00
@txlty 非常感谢
imcj
2013-12-17 10:51:52 +08:00
给出报价是最靠谱的做法。
lusin
2013-12-17 10:54:25 +08:00
lusin
2013-12-17 10:59:12 +08:00
@imcj 这个主要是以学习为主,自己费了半天劲搞不出来,想知道下原因。
买来这个东西也快不能用了,阿里妈妈最近会全部统一为用淘宝帐号登陆
imcj
2013-12-17 11:06:17 +08:00
@lusin 你要认为别人应该为免费你调试程序,这我也没什么好说的了。
lusin
2013-12-17 11:14:47 +08:00
@imcj 不知如何说这个问题,我从来没认为人们应该要做什么,你非要这样理解,我也没什么好说的。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/93440

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX