create pastes inside folder with pastebin.com api?

*爱你&永不变心* 提交于 2020-02-07 03:56:09

问题


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)


回答1:


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.
}


来源:https://stackoverflow.com/questions/57648625/create-pastes-inside-folder-with-pastebin-com-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!