php中的Reddit API返回糟糕的验证码以提交故事

时间:2011-09-15 21:34:39

标签: php api httprequest reddit

使用php for Reddit api提交故事会将错误的验证码作为错误返回。 我能够使用api登录并使用api完美地获取usermod和captcha。 理想情况下,如果reddit_session cookie被传递,它应该发布并且不会返回错误的验证码可以有人让我对此有所了解..

参考链接: https://github.com/reddit/reddit/wiki/API

<?php
$user = "";
$passwd = "";
$url = "http://www.reddit.com/api/login/".$user;

$r = new HttpRequest($url, HttpRequest::METH_POST);
$r->addPostFields(array('api_type' => 'json', 'user' => $user, 'passwd' => $passwd));

try {
    $send = $r->send();
    $userinfo = $send->getBody();
} catch (HttpException $ex) {
    echo $ex;
}

$arr = json_decode($userinfo,true);

$modhash = $arr['json']['data']['modhash'];
$reddit_session = $arr['json']['data']['cookie'];

$post = array('uh'=>$modhash,
               'kind'=>'link',
                'url'=>'yourlink.com',
                'sr'=>'funny',
                'title'=>'omog-asdfasf',
                'id'=>'newlink',
                'r'=>'funnyier',                
                'renderstyle'=> 'html'              
                );


$url = "http://www.reddit.com/api/submit";

// Upvote RoboHobo's comment :)
// Add user cookie data
$r->addCookies(array("reddit_session" => $reddit_session));
// Set URL to vote
$r->setUrl($url);
// Add vote information, found at http://wiki.github.com/talklittle/reddit-is-fun/api-all-functions
$r->setPostFields($post);
// Send request blindly


try {
    $userinfo = $r->send();
} catch (HttpException $ex) {
    echo $ex;   
}
pre($userinfo);
exit;

function pre($r){
echo "<pre />";
print_r($r);
}
?>

2 个答案:

答案 0 :(得分:5)

对于最近偶然发现这个问题并且仍然遇到这个问题的其他人:

上述问题已得到修复且工作正常但是如果您为reddit bot创建了一个新帐户并尝试提交故事,则会收到bad_captcha错误。新帐户必须提交验证码,直到他们获得一定数量的业力,因此这是您所看到的错误。尝试使用较旧的帐户请求,这可以解决您的问题。

答案 1 :(得分:3)

据我所知,目前在Reddit API中破解了CAPTCHA。他们最初使用的是过时的PyCAPTCHA并且是migrating to reCAPTCHA。从那以后,有issue使用api_type:json有一个解决方法,github上有人正在使用它。他还提供了解释/解决方案:

  

很简单,当需要&gt;验证码时,json(虽然不是jquery)结果应该包含captcha_id。通过captcha_id我的意思是完成一个类似于以下网址的部分:&gt; http://www.reddit.com/captcha/(captcha_id).png

     

我遇到的用例是尝试使用&gt; api_type:json通过api提交故事。我很好地通知我不存在的验证码不正确,但是,我&gt;然后必须向http://www.reddit.com/api/new_captcha发出请求以获取&gt; captcha_id。最后一次往返似乎没必要。