Speed up development with full-stack environments for every branch.

Learn More

Curl module example - Fetching url and saving the content to a file

Forked from Hello World.

345 Runs 2960 Views 1586 Copies
Saved

Saved

gtsolutions 373

gtsolutions
published 4 years ago

<?php

/**
 * Curl example - Fetching an url and saving the content found on that url to a file (example_homepage.txt)
 */
//initializes a new session and return a cURL handle for use with the curl_setopt(), curl_exec(), and curl_close() functions.
$ch = curl_init();
//open/create a file for writing
$fp = fopen("example_homepage.txt", "w");

// set the url to fetch 
curl_setopt($ch, CURLOPT_URL, "example.com");


//return the transfer (the result of curl_exec()) as a string instead of outputing directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//puts the fetching result to $fp file handler
curl_setopt($ch, CURLOPT_FILE, $fp);

// execute the curl session
curl_exec($ch);

// close curl resource to free up system resources 
curl_close($ch);

//close the file opened for writing
fclose($fp);

echo '<br /><center><div style="background-color:green;color:#fff;padding:10px;width:400px;font-size:16px">please check the file: <b>example_homepage.txt</b> to see the results!</div></center>';
?>

Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).