1

I need to post a json to a method in codeigniter 2.2.0, that post was made in a view with a jquery-ajax, like this:

function envia_mail_ajax(numero, form_data, ruta){  
var iddiv="#mail_"+numero;
window.setTimeout(
    function(){
    $.ajax({
            url: "<?php site_url('emailmasivo'); ?>/" +ruta+ "/" +numero, 
            cache: false,
            type: 'POST',
            data: form_data,
            dataType: "json",
            success: function(html){
                $( iddiv ).append(html.mensaje+"\n"+'<br />');
            }   
        });
    }, 600);

}

and it was used like this (within a loop over i):

envia_mail_ajax(i,
  {para:correos_e[i],id_masivos:id_masivos_e[i],id_mat_referencia:id_mat_referencia_e[i],
            id_tipouser:id_tipouser_e[i],nombre:nombres_e[i], sexo:sexos_e[i], matricula:matriculas_e[i], passa:passa_e[i],id_cuenta:cuenta_id},
                "<?php echo $r_ajax; ?>");

now, I´m writing all that in such a way that no view will be needed, in order to make it possible to run it from the terminal´s commands line, essentially, this is telling me to POST to the controller´s method "<?php echo site_url('emailmasivo'); ?>/" +ruta+ "/" +numero the data in form_data; to do this I wrote a method based in a lecture I found here POST json to PHP, my method is:

function procesaInfo($Arreglo, $numero){
$url = $Arreglo['r_ajax'];

$ch = curl_init(echo site_url('emailmasivo') . $url . "/" . $numero);

$jsonData = array(
    'para' => $Arreglo['lista_mails']['correos_e'][$numero],
    'id_masivos' => $Arreglo['lista_mails']['id_masivos_e'][$numero],
    'id_mat_referencia' => $Arreglo['lista_mails']['id_mat_referencia_e'][$numero],
    'id_tipouser' => $Arreglo['lista_mails']['id_tipouser_e'][$numero], 
    'nombre' => $Arreglo['lista_mails']['nombres_e'][$numero], 
    'sexo' => $Arreglo['lista_mails']['sexos_e'][$numero],
    'matricula' => $Arreglo['lista_mails']['matriculas_e'][$numero],
    'matriculas_e' => $Arreglo['lista_mails']['passa_e'][$numero],
    //'id_cuenta' => $Arreglo['lista_mails']['cuenta_id'][$numero]
    'id_cuenta' => $Arreglo['id_cuenta']
    );

    $jsonDataEncoded = json_encode($jsonData);

    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 
    $result = curl_exec($ch);
    }

the problem is that when I try to use that method like this:

function proceso_envia($nuevoArreglo){

$hola = "hola";
//echo $cuenta_id = $nuevoArreglo['id_cuenta'].PHP_EOL; 
//print_r($nuevoArreglo['lista_mails']['correos_e']);

if(count($nuevoArreglo['lista_mails']['correos_e']) != 0){  
    $j = 0; 
    for($i = $nuevoArreglo['ini'] ; $i < count($nuevoArreglo['lista_mails']['correos_e']) ; $i++){
        if($nuevoArreglo['lista_mails']['correos_e'][$i] != NULL){
            $j = $j+1;
            sleep(1);
            echo "si llega!".PHP_EOL;
            $this->procesaInfo($nuevoArreglo, $i);
        }
    }
  } 
}

it seems that no data are being POST to my method, and worst, not even the method is being reached, how do I know? well, I used an echo "I´m here!"; at the very beginning of the proceso_envia function, and nothing was displayed... am I doing it right? how do I post correctly data to a CI method in a controller? thanx i.a. for your help!

Sergio Varela
  • 53
  • 1
  • 9
  • 1
    Are you sure that the post request is even being made? also remove the `echo` from this `curl_init(echo site_url('emailmasivo')` you aren't needing to display the output – Bankzilla Feb 11 '15 at 03:42
  • According to the lecture I make reference to, the POST is executed with the curl_exec($ch), actually I think procesaInfo($nuevoArreglo, $i) is not even being called... any other idea? thanx! – Sergio Varela Feb 11 '15 at 04:49
  • 1
    [Try this question as it's pretty much the same](http://stackoverflow.com/a/15643608/970057) – Bankzilla Feb 11 '15 at 20:41
  • Thank you, pretty useful info! also, if someone falls over here, I found a solution here: [link](http://davidwalsh.name/curl-post) – Sergio Varela Feb 12 '15 at 19:23

0 Answers0