Help me make paste!

-2

Well, guys who run php, I'm developing a website ... But I have one problem:

$linhas = "";

function adcionar_linha($linha) {
$linhas .= $linha."%0D%0A";
}

function criar_paste() {
$curl_ch = curl_init();
curl_setopt($curl_ch, CURLOPT_URL, "http://paste.ee/api");
curl_setopt($curl_ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_ch, CURLOPT_POST, TRUE);
curl_setopt($curl_ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl_ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0");
curl_setopt($curl_ch, CURLOPT_POSTFIELDS, 'key=CENSURADO&description=AUTO DaviDEV&paste=Paste gerado pelos sistemas DaviDEV!%0D%0A%0D%0A' . $linhas .'&encrypted=0&expire=0&format=json');
$dadosSite = curl_exec($curl_ch);  
return json_decode($dadosSite)->{'paste'}->{'link'};
}

Then when I call this api:

adcionar_linha("teste");
echo 'Seu paste: '.criar_paste();

And the result is: link

HELP ME !!

    
asked by anonymous 14.05.2016 / 20:52

1 answer

2

You are not passing $linhas to the function.

This should resolve:

function adcionar_linha( $linha, $linhas ) {
   $linhas .= $linha."%0D%0A";
   return $linhas;
}
function criar_paste( $linhas ) {
    $curl_ch = curl_init();
    curl_setopt($curl_ch, CURLOPT_URL, "http://paste.ee/api");
    curl_setopt($curl_ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl_ch, CURLOPT_POST, TRUE);
    curl_setopt($curl_ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($curl_ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0");
    curl_setopt($curl_ch, CURLOPT_POSTFIELDS, 'key=CENSURADO&description=AUTO DaviDEV&paste=Paste gerado pelos sistemas DaviDEV!%0D%0A%0D%0A' . $linhas .'&encrypted=0&expire=0&format=json');
    $dadosSite = curl_exec($curl_ch);  
    return json_decode($dadosSite)->{'paste'}->{'link'};
}

And in the call:

$linhas = "";
adcionar_linha( "teste", $linhas );
echo 'Seu paste: '.criar_paste( $linhas );

Note that we are passing $linhas to both functions.

Using global

Another output would be to declare $linhas as global , but it's a nut solution, because if you use functions, it does not make much sense to depend on external data:

$linhas = "";
function adcionar_linha($linha) {
    global $linhas;
    $linhas .= $linha."%0D%0A";
}
function criar_paste() {
    global $linhas;
    $curl_ch = curl_init();
    curl_setopt($curl_ch, CURLOPT_URL, "http://paste.ee/api");
    curl_setopt($curl_ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl_ch, CURLOPT_POST, TRUE);
    curl_setopt($curl_ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($curl_ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0");
    curl_setopt($curl_ch, CURLOPT_POSTFIELDS, 'key=CENSURADO&description=AUTO DaviDEV&paste=Paste gerado pelos sistemas DaviDEV!%0D%0A%0D%0A' . $linhas .'&encrypted=0&expire=0&format=json');
    $dadosSite = curl_exec($curl_ch);  
    return json_decode($dadosSite)->{'paste'}->{'link'};
}
adcionar_linha("teste");
echo 'Seu paste: '.criar_paste();


Sanitizing data with urlencode() :

In both cases, make amends to avoid problems:

curl_setopt($curl_ch, CURLOPT_POSTFIELDS, 'key=CENSURADO&description=AUTO DaviDEV&
paste=Paste gerado pelos sistemas DaviDEV!%0D%0A%0D%0A'.urlencode($linhas).'&
encrypted=0&expire=0&format=json');

(line breaks are just for easy reading).

The urlencode() is for you not to have problems if you send something with special characters to the paste:

adcionar_linha( 'um=dois&tres');
    
14.05.2016 / 21:31