Basic example:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
By default curl_exec() prints the response straight to the browser, so CURLOPT_RETURNTRANSFER is needed to get the response back as a string instead.
Building GET parameters
$data = http_build_query($data_array);
$getUrl = $url . "?" . $data;
curl_setopt($ch, CURLOPT_URL, $getUrl);
Sending a POST request
$ch = curl_init();
$data = ['hello' => 'test', 'hey' => 'you'];
curl_setopt($ch, CURLOPT_URL, "example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
// curl_setopt($ch, CURLOPT_POST, true); // arguably the more idiomatic option
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
curl_close($ch);
CURLOPT_POSTFIELDS accepts either a URL-encoded string like 'para1=val1¶2=val2&...', or an array where keys are field names and values are field contents.
If the value is an array, the Content-Type header is automatically set to multipart/form-data.
Cookies
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
Proxy
Without authentication:
$proxy_string = $proxy->ip_address . ':' . $proxy->port;
curl_setopt($this->ch, CURLOPT_PROXY, $proxy_string);
With authentication:
$proxy_string = $proxy->ip_address . ':' . $proxy->port;
$proxy_auth = 'user:password';
curl_setopt($this->ch, CURLOPT_PROXY, $proxy_string);
curl_setopt($this->ch, CURLOPT_PROXYUSERPWD, $proxy_auth);
Disable:
curl_setopt($ch, CURLOPT_PROXY, null);
See also HTTP proxies for scraping (providers and checkers).
Snippets
Always check for errors after running a request:
if (!curl_errno($ch)) {
// ...
}
if (curl_errno($this->ch)) {
throw new Exception('Request error: ' . curl_error($this->ch));
}
Get the final URL after redirects:
$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
Get the HTTP response code:
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
Switch a handle back from POST to GET:
curl_setopt($ch, CURLOPT_HTTPGET, true);
Get response headers:
curl_setopt($ch, CURLOPT_HEADER, true);
$response = curl_exec($ch);
list($header, $body) = explode("\r\n\r\n", $response, 2);
Andrew Dorokhov