CURL PHP Ready function !

meabed 0 Tallied Votes 411 Views Share

This is CURL Ready function , to use it just call do post if the page you are calling has post variables , or get if it has get variables :)

$random=rand(1, 100000);
$cookie=$random . ".txt";
$agent="Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";

function doRequest($method, $url, $referer, $agent, $cookie, $vars) {
    $ch=curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    if($referer != "") {
	curl_setopt($ch, CURLOPT_REFERER, $referer);
    }
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
    if ($method == 'POST') {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
    }
    if (substr($url, 0, 5) == "https") {
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
    }
    $data = curl_exec($ch);
    curl_close($ch);
    if ($data) {
        return $data;
    } else {
        return curl_error($ch);
    }
}

function get($url, $referer, $agent, $cookie) {
    return doRequest('GET', $url, $referer, $agent, $cookie, 'NULL');
}

function post($url, $referer, $agent, $cookie,  $vars) {
    return doRequest('POST', $url, $referer, $agent, $cookie, $vars);
}
daniel36 -7 Light Poster

what is the use of this code?

cereal 1,524 Nearly a Senior Poster Featured Poster

@daniel you can use it to submit data to a script just as you're submitting a form, automatically. This script sends his request as a Firefox user, so it can bypass a filtering by User-Agent.

But it becomes useless if the receiving script expects also a captcha or a CSRF value.
The usage is simple:

$random=rand(1, 100000);
$cookie=$random . ".txt";
$agent="Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";

# where to submit data
$url = 'http://website.tld/data.php';

# the page from which is supposed to submit data
# this is fake information, just as User-Agent
$referer = 'http://website.tld/form.php';


# form input fields and values to submit
$vars = array(
'field_name_1' => 'value 1',
'field_name_2' => 'value 2'
);

echo post($url, $referer, $agent, $cookie,  $vars);

bye.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.