Simple cURL example

A simple example on how to use the cURL module to download a website or any other file. <br/> You can find more cURL options on the related PHP manual page.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
function curl_download($Url){
 
    // is cURL installed yet?
    if (!function_exists('curl_init')){
        die('Sorry cURL is not installed!');
    }
 
    // OK cool - then let's create a new cURL resource handle
    $ch = curl_init();
 
    // Now set some options (most are optional)
 
    // Set URL to download
    curl_setopt($chCURLOPT_URL$Url);
 
    // Set a referer
    curl_setopt($chCURLOPT_REFERER"http://www.example.org/yay.htm");
 
    // User agent
    curl_setopt($chCURLOPT_USERAGENT"MozillaXYZ/1.0");
 
    // Include header in result? (0 = yes, 1 = no)
    curl_setopt($chCURLOPT_HEADER0);
 
    // Should cURL return or print out the data? (true = return, false = print)
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
 
    // Timeout in seconds
    curl_setopt($chCURLOPT_TIMEOUT10);
 
    // Download the given URL, and return output
    $output = curl_exec($ch);
 
    // Close the cURL resource, and free system resources
    curl_close($ch);
 
    return $output;
}
//Example:
print curl_download('http://www.example.org/');
X

Url: http://www.jonasjohn.de/snippets/php/curl-example.htm

Language: PHP | User: ShareMySnippets | Created: Oct 16, 2013 | Tags: curl example