【问题标题】:post to tumblr using php使用 php 发布到 tumblr
【发布时间】:2011-08-24 07:13:54
【问题描述】:

谁能帮我弄清楚如何使用 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";
                }
        }

感谢您的帮助

【问题讨论】:

标签: php tumblr


【解决方案1】:

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

你需要的是TumblrOAuth,它是从OAuth Sandbox构建的。

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

【讨论】:

    【解决方案2】:

    盗自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";
    }
    
    ?>
    

    【讨论】:

    • 这似乎不起作用...... tumblr 已将身份验证过程更改为包括 oauth ... 我已经尝试过这个 .... 感谢您的努力和帮助,感谢您的回复跨度>
    • thumblr 现在似乎在使用 API v2,我没有找到脚本,但我设法找到了一些好的文档,tumblr api v2 #posting。如果您找到解决方案,请在此处发布以供将来帮助,我也会这样做。 GL :)
    • 我找到了一个Pyblr,一个用 python 编写的简单 Tumblr API v2 库,看看你能不能用它,它很新(2011 年 8 月 4 日)。
    • Tumblr 现在已经禁用了他们的旧 API (v1),所以这将不再起作用。
    【解决方案3】:
    $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 博客。

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

    【讨论】:

    • 你是如何尝试编码的?你能把它贴在这里以便我们提供帮助吗?
    • 您缺少 OAuth 类。
    • 您需要确保您的 php 版本启用了 OAuth 扩展。
    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 2014-02-16
      • 1970-01-01
      • 2011-09-13
      • 1970-01-01
      • 2012-09-09
      • 1970-01-01
      • 1970-01-01
      • 2013-12-08
      • 2023-04-10
      相关资源
      最近更新 更多