How to store the following curl_exec output into mysql table?

sent,success,114661698,709081462,+919900123456
sent,success,134707665,937687314,+917795987654
Eg:
var1='sent'
var2='success'
var3='114661698'
var4='709081462'
var5='+919900123456'

var1='sent'
var2='success'
var3='134707665'
var4='937687314'
var5='+917795987654'

Thanks in advance

Hi,

you have two choices:

  1. parse the CSV strings into an array and then insert
  2. use the LOAD DATA INFILE statement to feed the file to the database, but it requires to have access to a folder that is readable by the MySQL server

For an example of the first case see this thread on SO:

Basically you send the result of the curl execution to a temporary file and parse it through fgetcsv():

<?php

$url = 'http://localhost/return_csv.php';
$f   = fopen('php://temp', 'w+');
$ch  = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FILE, $f);
curl_exec($ch);
curl_close($ch);

rewind($f);

try {

    $db = new PDO('sqlite:./data.db');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->beginTransaction();

    $stmt = $db->prepare('INSERT INTO `test` (`var1`, `var2`, `var3`, `var4`, `var5`) VALUES(?, ?, ?, ?, ?)');

    while($csv = fgetcsv($f))
        $stmt->execute($csv);

    $db->commit();
}

catch(PDOException $e) {
    $db->rollback();
    die('Database error: ' . $e->getMessage() . PHP_EOL);
}

catch(Exception $e) {
    die('Error: ' . $e->getMessage() . PHP_EOL);
}

finally {
    fclose($f);
}

See:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.