在 pastebin.com 上创建文件夹后,如何使用 API 将粘贴上传到该文件夹? 我在 API 文档中真的找不到任何提及文件夹的内容 https://pastebin.com/api (事实上,folder
这个词根本没有出现在文档中,运行curl 'https://pastebin.com/api' -s | grep -i folder | wc -l
返回 0(,我想用 PHP + curl_ API 来做这件事,如果这很重要(但我怀疑它是否如此(
这显然不是一个好的解决方案,但它是迄今为止我找到的唯一解决方案(我也问过 pastebin.com 管理员电子邮件,但尚未得到回复(: 只是假装是浏览器,并绕过 API,因为浏览器可以上传到文件夹。 此实现使用 hhb_curl,但应该很容易移植到任何其他类似 curl 的 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.
}