2010-12-13 68 views
1
<?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中使用api将数据粘贴到pastebin?

+0

不太了解curl,但请记住它必须是一个'POST'请求​​,用'pastebin.com/api_public.php'设置变量'paste_code'。看起来你没有通过粘贴代码,也没有设置正确的参数。 – mpen 2010-12-13 07:44:19

回答

2

documentation说,你需要提交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

0

对于那些w在通过SEACH这个线程浩stumple,这里是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"; 
0

仅供参考别人看着这个“后2013”​​,该api_public.php POST已经停产。

相关问题