Help me make a stop!

Asked

Viewed 71 times

-2

Well, guys that php program, I’m developing a website... But I got a 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'};
}

That’s when I call this api:

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

And the result is: https://paste.ee/p/iFW59

HELP ME!!

  • What you wanted to happen, and what’s happening differently?

  • I switch to add a line( the line "test" ) .

1 answer

2


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');
  • Worked!!!!!!!!!!!!!!!!!!!!!!!!!!

  • Thank goodness... :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.