Problem with accentuation cURL

1

I have a code where I send data via cURL to another page inside the server, but I have a problem ... When sending this data to the other page, it inserts into MySQL only when viewing the record inserted in the MySQL all words after any accent goes blank. For example, if I send Programming in MySQL just enter Program the rest will not send.

cURL.php

<?php
header('Content-Type: application/json; charset=utf-8');
$ch = curl_init();

extract($_POST); //Recebe do $.post() do jQuery

$data = array('dados'=>array('nome'=>$nome, 'tipo'=>$tipo));

curl_setopt($ch, CURLOPT_URL, "http://localhost/app2/api/api.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

$output = curl_exec($ch);


echo json_decode($output);

curl_close($ch);
?>

api.php

<?php
...

$dados = $_POST["dados"];

$nome = $dados["nome"];
$tipo = $dados["tipo"];

$insere = mysql_query("INSERT INTO tabela VALUES (NULL, '".$nome."', '".$tipo."')");

...
?>
    
asked by anonymous 31.08.2014 / 23:48

1 answer

1

Have you tried using UTF8 conversion before sending to cURL?

Apparently it may not be necessary however values from a form may not be encoded in UTF-8 and this causes the problem.

Try this:

...
extract($_POST); //Recebe do $.post() do jQuery

$nome = utf8_encode($nome);
$tipo = utf8_encode($tipo);

$data = array('dados'=>array('nome'=>$nome, 'tipo'=>$tipo));
...

Another detail I noticed in your code is the use of extract in POST; This type of use is not recommended. The function documentation itself recommends that you do not use:

  

Warning   Do not use extract () on untrusted data, like user input (i.e. $ _GET, $ _FILES, etc.). If > you do, if you want to run old code that relies on register_globals > temporarily, make sure you use one of the non-overwriting flags values such as EXTR_SKIP & be aware that you should extract the same order that is defined in variables_order> within the php.ini. Source: PHP: extract - Manual

You can change the code and do so for more security:

...
//extract($_POST); //Recebe do $.post() do jQuery

$nome = utf8_encode($_POST['nome']);
$tipo = utf8_encode($_POST['tipo']);

$data = array('dados'=>array('nome'=>$nome, 'tipo'=>$tipo));
...

Success!

    
09.10.2014 / 18:48