ilqcho Posted November 10, 2022 Share Posted November 10, 2022 (edited) 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 November 10, 2022 by ilqcho security Quote Link to comment https://forums.phpfreaks.com/topic/315513-undefined-array-key-at-curl-post-method/ Share on other sites More sharing options...
Solution ginerjm Posted November 10, 2022 Solution Share Posted November 10, 2022 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'); Quote Link to comment https://forums.phpfreaks.com/topic/315513-undefined-array-key-at-curl-post-method/#findComment-1602389 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.