2010-12-13 60 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; 
?> 

Ceci est mon code php. où $ paste_data contient les données à coller dans une nouvelle page. Comment puis-je le coller en utilisant la fonction paste_code (String)?Comment coller des données dans pastebin en utilisant l'API en php?

+0

ne sait pas trop sur curl, mais gardez à l'esprit qu'il doit être une demande 'POST' à' http: // pastebin.com/api_public.php' avec la variable 'paste_code' ensemble. ne regarde pas que vous avez passé le code de collage, ni définir le bon paramètre. – mpen

Répondre

2

Le documentation dit que vous devez présenter une demande à POST

http://pastebin.com/api_public.php 

et le seul paramètre obligatoire est paste_code, de type chaîne est la pâte que vous voulez faire.

En cas de succès, une nouvelle URL pastebin sera renvoyée.

exemple de l'os nu:

$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; 

et marche-je obtenir:

> 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 

Il est clair que la réponse a l'URL http://pastebin.com/Lc7kAw8Z

Visitez et vous verrez une nouvelle pâte contenant hello there SO

0

Pour ceux w ho stumple sur ce fil via Seach, voici un code qui fonctionne en 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

Pour votre information pour les autres qui cherchent à ce "post 2013", le POST api_public.php a été arrêté.