使用 pastebin.com api 在文件夹内创建粘贴? [英] create pastes inside folder with pastebin.com api?

查看:79
本文介绍了使用 pastebin.com api 在文件夹内创建粘贴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 pastebin.com 上创建文件夹后,我如何使用 api 将粘贴上传到该文件夹​​?我在 https://pastebin.com/api 的 API 文档中找不到任何提及文件夹的内容(实际上,folder 这个词根本没有出现在文档中,运行 curl 'https://pastebin.com/api' -s | grep -i folder | wc -l 返回 0),如果这很重要,我想用 php + curl_ api 来做(但我怀疑它是)

after creating a folder on pastebin.com, how do i upload pastes to that folder with the api? i couldn't really find any mention of folders in the API documentation at https://pastebin.com/api (in fact, the word folder does not appear in the the documentation at all, running curl 'https://pastebin.com/api' -s | grep -i folder | wc -l returns 0), i want to do it with php + the curl_ api if that's significant (but i doubt it is)

推荐答案

这显然不是一个好的解决方案,但它是我迄今为止发现的唯一一个解决方案(我也问过 pastebin.com admin email,但还没有收到回复):假装是浏览器,完全绕过api,因为浏览器可以上传到文件夹.此实现使用 hhb_curl,但它应该易于移植到任何其他类似 curl 的 api.

this is obviously not a good solution, but it was the only one i have found thus far (i have also asked pastebin.com admin email, but haven't gotten a response yet): just pretend to be a browser, and bypass the api entierly, because browsers can upload to folders. this implementation uses hhb_curl, but it should be easy to port to any other curl-like api.

class Pastebin_to_folder_argument{
    public $username;
    public $password;
    public $folder_id;
    public $text;
    public $title="untitled";
    public $syntax_highlight_language=1;
    public $paste_expire="N";
    public $paste_exposure=1;
    public $paste_private=true;
}
$arg=new Pastebin_to_folder_argument();
$arg->username=USERNAME;
$arg->password=PASSWORD;
$arg->folder_id="rtUNY7pF"; 
$arg->text="test paste from PHP!";
var_dump(pastebin_to_folder($arg));
function pastebin_to_folder(Pastebin_to_folder_argument $arg):string
{
    $hc = new hhb_curl('', true);
    $html = $hc->setopt_array(array(
        CURLOPT_URL => 'https://pastebin.com/login',
        CURLOPT_POST => 1,
        CURLOPT_POSTFIELDS => http_build_query(array(
            'submit_hidden' => 'submit_hidden',
            'user_name' => $arg->username,
            'user_password' => $arg->password,
            'submit' => 'Login'
        ))
    ))->exec()->getStdOut();
    if (false === strpos($html, 'onclick="location.href=\'/logout\'"')) {
        ob_start();
        var_dump($html);
        $str = ob_get_clean();
        fwrite(STDERR, $str);
        throw new \RuntimeException("failed to login! (could not find the logout button - html printed to stderr for debugging.)");
    }
    $html=$hc->setopt(CURLOPT_HTTPGET,1)->exec('https://pastebin.com')->getStdOut();
    //hhb_var_dump($html) & die();
    //<input type="hidden" name="csrf_token_post" value="MTU2NjkxOTQ4MjJnRGxJMUowSjVMV1pYdU8yWUV3VVBNbVg2NXFlZmZs">
    $domd=@DOMDocument::loadHTML($html);
    $xp=new DOMXPath($domd);
    $csrf_token=$xp->query("//input[@name='csrf_token_post']")->item(0)->getAttribute("value");
    $max_file_size=$xp->query("//input[@name='MAX_FILE_SIZE']")->item(0)->getAttribute("value");
    $shitty_workaround_tmpfile=tmpfile();
    $shitty_workaround=new CURLFile(stream_get_meta_data($shitty_workaround_tmpfile)['uri'],"application/octet-stream","");

    $html=$hc->setopt_array(array(
        CURLOPT_URL=>'https://pastebin.com/post.php',
        CURLOPT_POST=>1,
        CURLOPT_POSTFIELDS=>array(
            'csrf_token_post'=>$csrf_token,
            'submit_hidden'=>'submit_hidden',
            'item_upload'=>$shitty_workaround, 
            'file_post'=>'',
            'MAX_FILE_SIZE'=>$max_file_size,
            'paste_code'=>$arg->text,
            'paste_format'=>$arg->syntax_highlight_language,
            'paste_expire_date'=>$arg->paste_expire,
            'paste_private'=>$arg->paste_private,
            'paste_folder'=>$arg->folder_id,
            'new_folder_name'=>null,
            'paste_name'=>$arg->title,
        )
    ))->exec()->getStdOut();
    unset($shitty_workaround);
    fclose($shitty_workaround_tmpfile);
    //hhb_var_dump($html);
    // TODO: verify that upload was actually successfull?
    return $hc->getinfo(CURLINFO_EFFECTIVE_URL); // the paste url.
}

这篇关于使用 pastebin.com api 在文件夹内创建粘贴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆