Hello,

Is it possible to get data from another web server (not local webserver) using php and by that I can create the php connection to store into local mysql ... ?

Any ideas?

Dani AI

Generated

Yes - , you can fetch data from another web server with PHP and insert it into a local MySQL database. pointed you in the right direction; here is a compact, practical pattern plus key cautions to make it reliable and safe.

Use a proper HTTP client so you can set timeouts, follow redirects and handle authentication. For JSON use json_decode, for XML use SimpleXML, and for HTTP prefer cURL for full control (PHP cURL manual). Parse with json_decode or SimpleXML. Use PDO or mysqli with prepared statements for safe, charset-correct inserts (PDO manual).

$ch = curl_init('https://api.example.com/data');
curl_setopt_array($ch, [
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 10,
  CURLOPT_HTTPHEADER => ['Accept: application/json','Authorization: Bearer TOKEN'],
]);
$json = curl_exec($ch);
curl_close($ch);
$data = json_decode($json, true);

$pdo = new PDO('mysql:host=localhost;dbname=db;charset=utf8mb4','user','pass',[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);
$pdo->beginTransaction();
$stmt = $pdo->prepare('INSERT INTO items(col1,col2) VALUES(:a,:b)');
foreach ($data as $row) {
  $stmt->execute([':a'=>$row['a'],':b'=>$row['b']]);
}
$pdo->commit();

Notes: enforce UTF-8, validate TLS (do not turn off verification), set sensible timeouts and retry/backoff, log failures, sanitize remote fields before DB writes, and run the import from a cron or queue worker. If using PHP streams instead of cURL, ensure allow_url_fopen is enabled in php.ini (allow_url_fopen setting).

Recommended Answers

All 2 Replies

Thanks for the info man :)

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.