Problem with accentuation Curl

Asked

Viewed 1,370 times

2

I have a code that sends data via cURL to another page inside the server, only I have a problem... When sending this data to the other page, it inserts it into Mysql only when seeing the record inserted in Mysql all words after any accent is empty. For example, if I send Programming in Mysql only inserts Program the rest does 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."')");

...
?>
  • 2

    [ Mandatory comment on deprecation of mysql_* and sql injection ]

  • But the $_POST arrives accentuated in the api.php? Or the problem is mysql_query?

  • everything arrives accentuated correctly

  • Your base ta utf8 too ? and your connection is set to?

  • So it’s not Curl problem, but Mysql problem.

  • The problem is that I was with utf8_decode($variavel) before inserting into the seat, and doing so he would place an invalid character in the seat’s place and not insert correctly. Thank you all!

  • 1

    Put this as a solution to your question, share your bread with others, rs @Alissonacioli

Show 2 more comments

1 answer

1


Already tried using UTF8 conversion before sending to Curl ?

Apparently it may not be necessary but the values coming 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 the POST; This type of use is not recommended. The function documentation itself recommends non-use:

Warning Do not use Extract() on untrusted data, like user input (i.e. $_GET, $_FILES, etc.). If >you do, for example 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 >and be Aware that you should Extract in the same order that’s defined in variables_order >Within the php.ini. Source: PHP: Extract - Manual

You can change the code and do so to have 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!

Browser other questions tagged

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