Jump to content

Recommended Posts

  • Administrator

Lucrez la o metoda prin care puteti sa trimiteti catre api prin POST o lista cu multiple servere si aceasta returneaza inapoi json.

Link-ul este gapi.pbcv.dev/get/multiple si lista trebuie trimisa prin POST folositi ce vreti voi php curl, nodejs, python, etc.

 

Exemplu de script PHP, acesta ia serverele din baza de date apoi tirmite lista cu toate catre api.

Datele primine inapoi din api sunt stocate in $response de acolo faceti ce vreti voi cu ele, update la db-ul vostru etc.

<?php
// Start the timer
$startTime = microtime(true);

$dbHost = 'localhost';
$dbUser = 'user';
$dbPass = 'pass';
$dbName = 'dbname';
$charset = 'utf8mb4';

$dsn = "mysql:host=$dbHost;dbname=$dbName;charset=$charset";
$options = [
  PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
  PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  PDO::ATTR_EMULATE_PREPARES   => false,
];

try {
  $pdo = new PDO($dsn, $dbUser, $dbPass, $options);
} catch (\PDOException $e) {
  throw new \PDOException($e->getMessage(), (int)$e->getCode());
}

try {
  $stmt = $pdo->query('SELECT server FROM servers'); // Modificati dupa cum aveti voi setata coloana cu ip:port
  $servers = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);

  $jsonData = json_encode(array("servers" => $servers));
  $url = 'https://gapi.pbcv.dev/get/multiple';

  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($jsonData)
  ));

  $response = curl_exec($ch);

  if (curl_errno($ch)) {
    echo 'cURL error: ' . curl_error($ch);
  } /* else {
    //header('Content-Type: application/json');
    echo $response;
  } */

  curl_close($ch);

} catch (\PDOException $e) {
  error_log('Select query error: ' . $e->getMessage());
}

// End the timer and calculate the duration
$endTime = microtime(true);
$executionTime = $endTime - $startTime;
echo "\nExecution time: " . $executionTime . " seconds";

 

Link to comment
Share on other sites

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.   Paste as plain text instead

  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...