Pastebin API Paste Data

送分小仙女□ 提交于 2020-05-14 02:29:24

问题


I've been trying to get the pastebin API to instead of telling me the pastebin link , just output the raw data. The PHP code is this :

<?php

$api_dev_key        = 'Stackoverflow(fake key)';    
$api_paste_code         = 'API.'; // your paste text
$api_paste_private      = '1'; // 0=public 1=unlisted 2=private
$api_paste_expire_date  = 'N';
$api_paste_format       = 'php';
$api_paste_code     = urlencode($api_paste_code);

$url            = 'http://pastebin.com/api/api_post.php';
$ch             = curl_init($url);

?>

Normally this would upload the $api_paste_code into pastebin , showing up like pastebin.com/St4ck0v3RFL0W , but instead I want it to generate the raw data.

The raw data link is http://pastebin.com/raw.php?i= , can anyone help?

Reference : http://pastebin.com/api


回答1:


First off, please note that you must send a POST request to the pastebin.com API, not GET. So don't use urlencode() on your input data!

To get the raw paste url from the page url, you have several options. But the easiest is probably:

$apiResonse = 'http://pastebin.com/ABC123';
$raw        = str_replace('m/', 'm/raw.php?i=', $apiResponse);

Finally, here is a complete example:

<?php
$data = 'Hello World!';

$apiKey  = 'xxxxxxx';                         // get it from pastebin.com
$apiHost = 'http://pastebin.com/';

$postData = array(
    'api_dev_key'    => $apiKey,             // your dev key
    'api_option'     => 'paste',             // action to perform
    'api_paste_code' => utf8_decode($data),  // the paste text
);

$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_URL             => "{$apiHost}api/api_post.php",
    CURLOPT_RETURNTRANSFER  => 1,
    CURLOPT_POST            => 1,
    CURLOPT_POSTFIELDS      => http_build_query($postData),
));

$result = curl_exec($ch); // on success, some string like 'http://pastebin.com/ABC123'
curl_close($ch);

if ($result) {
    $pasteId = str_replace($apiHost, '', $result);
    $rawLink = "{$apiHost}raw.php?i={$pasteId}";

    echo "Created new paste.\r\n Paste ID:\t{$pasteId}\r\n Page Link:\t{$result}\r\n Raw Link:\t{$rawLink}\r\n";
}

Running the above code, outputs:

c:\xampp\htdocs>php pastebin.php
Created new paste.
 Paste ID:      Bb8Ehaa7
 Page Link:     http://pastebin.com/Bb8Ehaa7
 Raw Link:      http://pastebin.com/raw.php?i=Bb8Ehaa7



回答2:


As far as I see, the response contains the Pastebin URL generated when the content is created. An Url like this:

http://pastebin.com/UIFdu235s

So what you only need is to get rid of "http://pastebin.com/" doing:

$id = str_replace("http://pastebin.com/", "", $url_received_on_last_step);

And then, append it to the raw url you provided:

$url_raw = "http://pastebin.com/raw.php?i=".$id;

And you'll get the raw data.



来源:https://stackoverflow.com/questions/13041282/pastebin-api-paste-data

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