📢 Nuovo Corso Bootstrap Completo disponibile!

Esercizi cURL PHP

Ecco degli esercizi con soluzione per praticare l’uso di cURL in PHP.

Esercizio 1: Fare una Richiesta GET

Utilizzare cURL per fare una richiesta GET a un'API e stampare la risposta.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.chucknorris.io/jokes/random");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response; // Output: JSON response with a Chuck Norris joke
?>

Esercizio 2: Fare una Richiesta POST

Utilizzare cURL per fare una richiesta POST a un'API e stampare la risposta.
<?php
$data = array(
"name" => "Mario",
"email" => "mario@example.com"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://jsonplaceholder.typicode.com/posts");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response; // Output: JSON response with the created post
?>

Esercizio 3: Impostare Headers Personalizzati

Utilizzare cURL per fare una richiesta GET con headers personalizzati.
<?php
$ch = curl_init();
$headers = [
'Content-Type: application/json',
'Authorization: Bearer YOUR_ACCESS_TOKEN'
];
curl_setopt($ch, CURLOPT_URL, "https://api.example.com/data");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response; // Output: Response from the API
?>

Esercizio 4: Gestire gli Errori di cURL

Utilizzare cURL per fare una richiesta GET e gestire eventuali errori.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.example.com/data");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if(curl_errno($ch)) {
echo 'Errore cURL: ' . curl_error($ch);
} else {
echo $response; // Output: Response from the API
}
curl_close($ch);
?>

Esercizio 5: Inviare Dati JSON con cURL

Utilizzare cURL per fare una richiesta POST inviando dati in formato JSON.
<?php
$data = array(
"name" => "Mario",
"email" => "mario@example.com"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://jsonplaceholder.typicode.com/posts");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response; // Output: JSON response with the created post
?>

Esercizio 6: Scaricare un File con cURL

Utilizzare cURL per scaricare un file da un URL e salvarlo localmente.
<?php
$url = "https://www.example.com/file.zip";
$destinazione = "file.zip";
$ch = curl_init($url);
$fp = fopen($destinazione, 'w+');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);
echo "File scaricato con successo!";
?>

Esercizio 7: Ottenere Informazioni sull’Operazione cURL

Utilizzare cURL per fare una richiesta GET e ottenere informazioni dettagliate sull'operazione.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.chucknorris.io/jokes/random");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if($response === false) {
echo 'Errore cURL: ' . curl_error($ch);
} else {
$info = curl_getinfo($ch);
echo "Tempo di esecuzione: " . $info['total_time'] . " secondi<br>";
echo "Dimensione dei dati scaricati: " . $info['size_download'] . " bytes<br>";
echo "Risposta HTTP: " . $info['http_code'] . "<br>";
echo "URL finale: " . $info['url'] . "<br>";
}
curl_close($ch);
?>