繁体   English   中英

使用php发布到tumblr

[英]post to tumblr using php

谁能帮我弄清楚如何使用php发布到tumblr。 我尝试使用Google搜索某个库或示例代码,但找不到一个。 我只能在这里找到这里https://github.com/alexdunae/tumblr-php/blob/master/Tumblr.php ,它似乎不起作用,我也在tumblr网站上的v1 api上查看并尝试了该代码,但该代码不起作用要么....

 function post($data){
                if(function_exists("curl_version")){
                        $data["email"] = $this->email;
                        $data["password"] = $this->password;
                        $data["generator"] = $this->generator;
                        $request = http_build_query($data);
                        $c = curl_init('http://www.tumblr.com/api/write');
                        curl_setopt($c,CURLOPT_POST,true);
                        curl_setopt($c,CURLOPT_POSTFIELDS,$request);
                        curl_setopt($c,CURLOPT_RETURNTRANSFER,true);
                        $return = curl_exec($c);
                        $status = curl_getinfo($c,CURLINFO_HTTP_CODE);
                        curl_close($c);
                        if($status == "201"){
                            return true;
                        }
                        elseif($status == "403"){
                            return false;
                        }
                        else{
                            return "error: $return";
                        }
                }
                else{
                        return "error: cURL not installed";
                }
        }

谢谢您的帮助

我只是注意到这显示为Tumblr的精选集,我想这样说:从2012年开始,您应该忽略Tuga的上述回答,因为它不适用于最新的Tumblr API。

您需要的是从OAuth Sandbox构建的TumblrOAuth

它仅用于读写Tumblr帖子,因此,如果您想做更多的事情,则需要更改代码。 我将其用作Follower的代码库。

$conskey = "CONSUMER KEY";
$conssec = "CONSUMER SECRET";

$tumblr_blog = "myblog.tumblr.com";
$to_be_posted = "This is the text to be posted";

$oauth = new OAuth($conskey,$conssec);
$oauth->fetch("http://api.tumblr.com/v2/blog/".$tumblr_blog."/post", array('type'=>'text', 'body'=>$to_be_posted), OAUTH_HTTP_METHOD_POST);

$result = json_decode($oauth->getLastResponse());

if($result->meta->status == 200){
  echo 'Success!';
}

此代码将使您可以使用tumblr API将其发布到tumblr博客。

我希望这段代码对您有所帮助。

http://www.tumblr.com/docs/en/api被盗

// Authorization info
$tumblr_email    = 'info@davidville.com';
$tumblr_password = 'secret';

// Data for new record
$post_type  = 'regular';
$post_title = 'The post title';
$post_body  = 'This is the body of the post.';

// Prepare POST request
$request_data = http_build_query(
    array(
        'email'     => $tumblr_email,
        'password'  => $tumblr_password,
        'type'      => $post_type,
        'title'     => $post_title,
        'body'      => $post_body,
        'generator' => 'API example'
    )
);

// Send the POST request (with cURL)
$c = curl_init('http://www.tumblr.com/api/write');
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);

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

?>

Tuga提供的api示例正在为我工​​作(在Wordpress上)...所以我认为您的问题出在其他地方,而不是提供的示例。 如果你们能够发布版本2的API,我也将不胜感激。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM