You are not passing $linhas
for the function.
That should solve:
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 on the call:
$linhas = "";
adcionar_linha( "teste", $linhas );
echo 'Seu paste: '.criar_paste( $linhas );
Notice that we are passing $linhas
for both functions.
Using global
Another way out would be to declare $linhas
as global
, but it’s a nut solution, because if you’re going to use functions, it doesn’t make much sense for them 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 the data with urlencode()
:
In both cases, it pays to make this change 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 only for easy reading).
The urlencode()
serves to you not to have problems if send something with special characters to the Paste:
adcionar_linha( 'um=dois&tres');
What you wanted to happen, and what’s happening differently?
– Bacco
I switch to add a line( the line "test" ) .
– Davi Moura