PHP and CURL

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
function browse($url, $postData = null) {
    $fields = '';
    if(is_array($postData) && sizeof($postData) >= 1){
        $fields = http_build_query($postData);
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    if($fields !=='') {
        curl_setopt($ch, CURLOPT_POST, count($postData));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    }

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    curl_setopt($ch, CURLOPT_COOKIESESSION, true);
    // Write cookie if needed  .. also try:  dirname(__FILE__) . '/cookie.txt';
    curl_setopt($ch, CURLOPT_COOKIEJAR , '/tmp/cookie.txt');
    // Read cookie if needed
    curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookie.txt');

    $result = curl_exec($ch);
    curl_close($ch);

    return $result;
}
This entry was posted in php, Web Development. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


2 − one =