Archive
This post is archived and may contain outdated information. It has been set to 'noindex' and should stop showing up in search results.
Simple Beginner Guide for Using PHP cURL
Apr 10, 2015ProgrammingComments (0)
cURL has a lot of configuration options and much more power than file_get_contents, but it can be daunting to learn. This guide is designed to teach you cURL from its most basic steps, and build onto it slowly so that you can understand it easily.


Basic Usage


Here's the most basic example of using cURL. This gets the content of a URL and outputs it (echo's it) automatically.

// The URL that you want to access
$url = 'http://www.h3xed.com/';

// Initialize cURL with the URL as only argument
$curl = curl_init($url);

// Execute cURL
curl_exec($curl);

Test it and it should display the content (source HTML code) of the URL.

If you don't want cURL to output the content directly, and instead return it so that you can store it into a variable (like how file_get_contents works), add this one option setting:

$url = 'http://www.h3xed.com/';
$curl = curl_init($url);

// Setting this option tells cURL to return the content
// of the URL instead of outputting it directly
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

// Execute cURL and store the content in a variable
$html = curl_exec($curl);

// You can now do what you want with the content
echo htmlspecialchars($html);


More Options


Here are some additional options you can use with cURL:

$url = 'http://www.h3xed.com/';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

// Setting this options tells cURL to follow any redirects.
// For example: http://microsoft.com/ redirects to
// http://www.microsoft.com/en-us/default.aspx
// This follows the redirects to get the final page
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

// Setting this option allows you to specify the
// user-agent you want cURL to use
curl_setopt($curl, CURLOPT_USERAGENT, 'Custom User Agent');

// Use these options if you want to send POST data
// $post should be a normal key value pairing like:
// key1=value1&key2=value2
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);

// This option allows you to control how much time (in
// seconds) curl_exec will block. If the URL doesn't
// respond by then, it will return false
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 6);

$html = curl_exec($curl);
echo $html;


Wrapping It in a Function


It can be helpful to wrap your cURL implementation in a function like so:

function urlGetContents($url, $post = null, $agent = null) {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 6);
if ($post) {
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
}
if ($agent) {
curl_setopt($curl, CURLOPT_USERAGENT, $agent);
}
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
Comments (0)
Add a Comment
No comments yet