如何使用php中的api将数据粘贴到pastebin?

发布于 2024-10-07 04:24:12 字数 643 浏览 5 评论 0原文

<?php 

/* gets the data from a URL */ 
function get_data($url) 

{ 

  $ch = curl_init();

  $timeout = 5;

  curl_setopt($ch,CURLOPT_URL,$url);

  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);

  $data = curl_exec($ch);

  curl_close($ch);
  return $data;

}
$paste_data=""; if(isset($_POST["paste_code"])) { $paste_data = $_POST["paste_code"]; }
echo $paste_data;
$returned_content = get_data('http://pastebin.com/api_public.php/paste_code(paste_data)');
echo $returned_content;
?>

这是我的 php 代码。其中 $paste_data 包含要粘贴到新页面中的数据。如何使用函数paste_code(String)粘贴它?

<?php 

/* gets the data from a URL */ 
function get_data($url) 

{ 

  $ch = curl_init();

  $timeout = 5;

  curl_setopt($ch,CURLOPT_URL,$url);

  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);

  $data = curl_exec($ch);

  curl_close($ch);
  return $data;

}
$paste_data=""; if(isset($_POST["paste_code"])) { $paste_data = $_POST["paste_code"]; }
echo $paste_data;
$returned_content = get_data('http://pastebin.com/api_public.php/paste_code(paste_data)');
echo $returned_content;
?>

This is my php code . where $paste_data contains the data to be pasted in a new page . How do I paste it using the function paste_code(String) ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧竹 2024-10-14 04:24:12

文档 表示您需要提交一个 POST 请求,

http://pastebin.com/api_public.php

并且唯一强制参数是paste_code,字符串类型是您要制作的粘贴。

成功后,将返回新的 pastebin URL。

简单的例子:

$ch = curl_init("http://pastebin.com/api_public.php");
curl_setopt ($ch, CURLOPT_POST, true);

// A new paste with the string "hello there SO"
curl_setopt ($ch, CURLOPT_POSTFIELDS, "paste_code=hello there SO");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_NOBODY, 0);

$response = curl_exec($ch);

echo $response; 

运行时我得到:

> POST http://pastebin.com/api_public.php HTTP/1.1
Host: pastebin.com
Accept: */*
Proxy-Connection: Keep-Alive
Content-Length: 25
Content-Type: application/x-www-form-urlencoded

< HTTP/1.1 200 OK
< Transfer-Encoding: chunked
< Date: Mon, 13 Dec 2010 07:51:12 GMT
< Content-Type: text/plain
< Server: nginx/0.8.52
< Vary: Accept-Encoding
< X-Powered-By: PHP/5.3.4-dev
< Via: 1.1 apac-nc06 (NetCache NetApp/6.0.6)
< 
http://pastebin.com/Lc7kAw8Z* Closing connection #0

显然,响应的 URL 为 http://pastebin.com/Lc7kAw8Z

访问它,你会看到一个新的粘贴,其中包含 hello there SO

The documentation says that you need to submit a POST request to

http://pastebin.com/api_public.php

and the only mandatory parameter is paste_code, of type string is the paste that you want to make.

On success a new pastebin URL will be returned.

Bare bone example:

$ch = curl_init("http://pastebin.com/api_public.php");
curl_setopt ($ch, CURLOPT_POST, true);

// A new paste with the string "hello there SO"
curl_setopt ($ch, CURLOPT_POSTFIELDS, "paste_code=hello there SO");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_NOBODY, 0);

$response = curl_exec($ch);

echo $response; 

and on running I get:

> POST http://pastebin.com/api_public.php HTTP/1.1
Host: pastebin.com
Accept: */*
Proxy-Connection: Keep-Alive
Content-Length: 25
Content-Type: application/x-www-form-urlencoded

< HTTP/1.1 200 OK
< Transfer-Encoding: chunked
< Date: Mon, 13 Dec 2010 07:51:12 GMT
< Content-Type: text/plain
< Server: nginx/0.8.52
< Vary: Accept-Encoding
< X-Powered-By: PHP/5.3.4-dev
< Via: 1.1 apac-nc06 (NetCache NetApp/6.0.6)
< 
http://pastebin.com/Lc7kAw8Z* Closing connection #0

Clearly the response has the URL http://pastebin.com/Lc7kAw8Z

Visit it and you'll see a new paste containing hello there SO

奶气 2024-10-14 04:24:12

供其他查看此“2013 年后”的人参考,api_public.php POST 已停止。

FYI for others looking at this "post 2013", the api_public.php POST has been discontinued.

泪眸﹌ 2024-10-14 04:24:12

对于那些通过 seach 偶然发现这个线程的人,这里有一个在 2013 年运行的代码:

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

$apiKey = 'xxxxxxx'; // get it from 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
    'api_paste_private'     => '1',                 // 0=public 1=unlisted 2=private
    'api_paste_expire_date' => '1D',                // paste expires in 1 day
);

$ch = curl_init('http://pastebin.com/api/api_post.php');
curl_setopt_array($ch, array(
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => http_build_query($postData),
    CURLOPT_RETURNTRANSFER  => 1,
));
$re = curl_exec($ch);
curl_close($ch);

$pasteId = end(explode('/', $re));
echo "Created new paste.\r\n Link:\t{$re}\r\n Raw:\t" . sprintf('http://pastebin.com/raw.php?i=%s', $pasteId) . "\r\n";

For those who stumple upon this thread via seach, here is a code that works in 2013:

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

$apiKey = 'xxxxxxx'; // get it from 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
    'api_paste_private'     => '1',                 // 0=public 1=unlisted 2=private
    'api_paste_expire_date' => '1D',                // paste expires in 1 day
);

$ch = curl_init('http://pastebin.com/api/api_post.php');
curl_setopt_array($ch, array(
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => http_build_query($postData),
    CURLOPT_RETURNTRANSFER  => 1,
));
$re = curl_exec($ch);
curl_close($ch);

$pasteId = end(explode('/', $re));
echo "Created new paste.\r\n Link:\t{$re}\r\n Raw:\t" . sprintf('http://pastebin.com/raw.php?i=%s', $pasteId) . "\r\n";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文