如何通过身份验证将CURL连接到Tumblr API

时间:2011-04-07 11:03:37

标签: authentication curl tumblr

我偶然发现了使用经过身份验证的连接的问题。我不想回复标有特定标签的私人帖子。 目前,我收到状态200,显示空错误消息,无法执行任何操作。 代码(几乎与您的文档相同:

$request_data = http_build_query(array('email'=>$thumblr['email'],'password'=>$thumblr['pass']));
//$c = curl_init("http://".$thumblr['acc_name'].".tumblr.com/api/read?tagged=".$thumblr['the_tag']);
$c = curl_init("http://www.tumblr.com/api/authenticate");
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $request_data);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($c);
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);

if ($status == 201) {
echo "Success! The new post ID is $result.\n";
} else if ($status == 403) {
echo 'Bad email or password';
} else {
echo "Error [$status]: $result\n";
}

输出是:

Error [200]:

帮助? :)

1 个答案:

答案 0 :(得分:2)

Damnit,我自己拿到了......

$request_data = http_build_query(
    array(
        'email'        =>$thumblr['email'],
        'password'  =>$thumblr['pass'],
        'id'             =>$thumblr['header_id'],
        'tagged'      =>$thumblr['the_tag']
    )
);
$c = curl_init('http://'.$thumblr['acc_name'].'.tumblr.com/api/read');
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $request_data);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($c);
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);

switch ($status) {
    case '200': $msg = 'OK'; break;
    case '201': $msg = "Created - Success! The new post ID is $result.\n"; break;
    case '400': $msg = 'Bad Request - There was at least one error while trying to save your post.'; break;
    case '403': $msg = 'Forbidden - Your email address or password were incorrect.'; break;
    default: $msg = $result; break;
}
echo '<p>'.$msg.'</p><pre>'.print_r($result,1).'</pre>';