Jump to content

Undefined array key at curl POST method


ilqcho
Go to solution Solved by ginerjm,

Recommended Posts

Hello, I need to do a migration of data by posting it from my tables database to an API. I am trying to use curl for the REST setup. Here is an example of my code:

<?php
    include 'config.php';

	function api($url,$data) {
        //API REST
        $fields_string = http_build_query($data);

        print_r($fields_string);  

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "http://proyectodata/api/$url.php");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string );
        $resp = curl_exec($ch);
        curl_close($ch);
        var_dump($resp).'<br/>';

    }
    //id of the promo
    $promo = 33;
    //number of registers to transfer per load
    $cant = 50; 

    if (isset($_SESSION['indexp'])) {
        for ($i=0;$i<10;$i++) {    
            $ini = intval($_SESSION['indexp']);     

            //registers to process during this load
            $registers = $sql->query("SELECT * FROM beneficios LIMIT $ini,$cant");
            $_SESSION['indexp'] = $ini+$cant;

            //migration info
            echo "<p>ini: $ini / users: $registers->num_rows</p>";   

            $todos = array();
            while ($user = $registers->fetch_object()) {
                //set of general data
                $data = array('id' => $user->id, 'participacion' => $user->participacion, 'beneficio' => $user->beneficio,'fecha' => $user->fecha, 	'promo' => $promo);
                echo $sql->error;

                //added to an array for the data load
                array_push($todos,$data);
            }

            //call to API
            api('bulk',$todos);
        }
    ?>

The $data variable contains the data I need to post to the API but when I check the network ar my programmers tool the API is never called.
When I print_r the variable $fields_strin at my api() function I get the following warning message Warning: Undefined array key "indexp" on line 12.
My config.php file has the database config plus the initialization of the session like this: 

session_start();

$_SESSION['indexp'];

Sorry for the long post and code but I am new at PHP and was given this task with an example function (similar to this one that I edited) to do the migration.

Edited by ilqcho
security
Link to comment
Share on other sites

  • Solution
1 hour ago, ilqcho said:
session_start();

$_SESSION['indexp'];

What is the second line above supposed to be doing for you?  It is not a usable statement.  If you had error checking enabled you would probably get a message.  Add this to the start of your script, right after the session_start:

error_reporting(E_ALL);
ini_set('display_errors', '1');
 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.