PHP cURL
From Leo's Notes
Last edited on 30 May 2017, at 21:13.
Basic Usage
First create a curl instance using the curl_init
function. Second, define any options using the curl_setopt
function. Finally, fire the request using curl_exec
and close with curl_close
.
Example:
// create a new cURL resource
$c = curl_init();
// Set request options
curl_setopt($c, CURLOPT_URL, "http://labs.steamr.com/");
curl_setopt($c, CURLOPT_HEADER, 0);
// Run the request
curl_exec($c);
// close the resource
curl_close($c);
curl_exec
will by default output the contents of the request. The request above will not provide the HTTP request with a user agent string and will show up as an empty string on the remote host.
Setting Headers
Something a little more advanced
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://workrooms.ucalgary.ca/process_roombookings.php?m=calscroll&gid=13071&date=" . $date);
curl_setopt($ch, CURLOPT_REFERER, "http://workrooms.ucalgary.ca/booking/eeel");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$html = curl_exec($ch);