2012-10-24 44 views
0

我一直在試圖讓pastebin API代替告訴我pastebin鏈接,只是輸出原始數據。 PHP代碼是這樣的:Pastebin API粘貼數據

<?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); 

?> 

通常這會上傳$ api_paste_code到引擎收錄,可見像pastebin.com/St4ck0v3RFL0W了,而是我希望它生成的原始數據。

原始數據鏈接是http://pastebin.com/raw.php?i=,任何人都可以幫忙嗎?

參考:http://pastebin.com/api

回答

0

首先請注意,您必須發送POST請求到pastebin.com API,而不是GET。因此,請勿在輸入數據上使用urlencode()

要從頁面網址獲取原始粘貼網址,您有幾個選項。但最簡單的大概是:

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

最後,這裏是一個完整的例子:

<?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"; 
} 

運行上面的代碼,輸出:

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 
1

據我看到的,響應包含在創建內容時所產生的引擎收錄URL。像這樣的網址:

http://pastebin.com/UIFdu235s 

所以你只需要的是擺脫「http://pastebin.com/」這樣做的:

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

,然後將其追加到原始網址你提供了:

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

你會得到原始數據。

+0

沒有工作,仍然出來,而不原始數據。 – user1725113

+0

參考:http://snag.gy/tmqN0.jpg – user1725113

+0

但是你從哪裏得到'$ url_received_on_last_step'?我看不到您的代碼(來自所提供的圖片),您實際上已經獲取了收到的網址。 –