使用 php 发布到 tumblr

发布于 2024-12-01 06:26:56 字数 1540 浏览 1 评论 0原文

谁能帮我弄清楚如何使用 php 向 tumblr 发帖。 我尝试在谷歌上搜索库或示例代码,但找不到。我能找到的就是这里 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";
                }
        }

感谢您的帮助

Could anyone help me figure out how to post to tumblr using php.
I tried googling for a library or a sample code but couldn't find one. all I can find is this here https://github.com/alexdunae/tumblr-php/blob/master/Tumblr.php and it doesnt seem to work also I looked and tried the code on v1 api at tumblr website that doesnt work either ....

 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";
                }
        }

Thanks for the help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

埋葬我深情 2024-12-08 06:26:56

我刚刚注意到这显示为 Tumblr 的精选,我想说的是:截至 2012 年,您应该忽略 Tuga 的上述答案,因为它不适用于最新的 Tumblr API。

您需要的是 TumblrOAuth ,它是由 OAuth 沙箱

它仅用于读取和写入 Tumblr 帖子,因此如果您想要执行更多操作,则需要更改代码。我用它作为 Followr 的代码库。

I just noticed that this is showing up as Featured for Tumblr and I want to say this: As of 2012, you should IGNORE the above answer by Tuga because it DOES NOT work with the newest Tumblr API.

What you need is TumblrOAuth which is built from OAuth Sandbox.

It is only setup to read and write Tumblr posts, so if you want to do more than that, you'll need to alter the code. I used it as my code base for Followr.

演多会厌 2024-12-08 06:26:56

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

// Authorization info
$tumblr_email    = '[email protected]';
$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";
}

?>

Stolen from http://www.tumblr.com/docs/en/api

// Authorization info
$tumblr_email    = '[email protected]';
$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";
}

?>
在梵高的星空下 2024-12-08 06:26:56
$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 博客发布内容。

我希望这段代码有帮助。

$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!';
}

This code will let you post to your tumblr blog using tumblr API.

I hope this code helps.

溺深海 2024-12-08 06:26:56

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

The api example provided by Tuga is working for me (on Wordpress)...so I think your problem lies elsewhere, and not with the example provided. I would also be very appreciative if you guys got a version 2 api working if you could post it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文