Wednesday, 3 January 2018

Post Form Data with CURL and get Results

Today, I am doing another domain name related project(on of my specialties). I need a fresh PHP function to get a domain name's whois data from. It's time to get dirty with CURL because all my PHP whois functions are failing lately after about 500 queries or less, so let's do this!

PHP CURL POST FUNCTION

The first thing I needed was a decent CURL function to post to a URL and get results. Here is what I found:
<?php
function curlPost($url, $data){
// check for URL and data:
    if(empty($url)){
        return 'Error: missing URL';
    }
    if(empty($data)){
        return 'Error: missing Data';
    }
//get the data for the POST and put in URL:
$fields_string = '';
foreach($data as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//Set the URL, no. of POST vars and POST data:
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($data));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//timeout after 10 secs(change as needed):
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10);
//curl_setopt($ch,CURLOPT_HEADER,false);
//Set to return data instead of printing:
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
//pass user agent:
curl_setopt($ch,  CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//execute post:
$result = curl_exec($ch);
//close connection:
curl_close($ch);
return $result;
}//end curlPost function.
echo "<h3>Whois test</h3>";
$data = array('d43b' => 'jafty.com');
echo curlPost('http://www.secret.net/services/whois.php', $data);
?>
Okay, I know, unfair, I took out the target domain, but find your own lol. I don't want everyone exploiting the poor server. But I was still limited with this technique because the server I queried had a maximum number of queries allowed, so I still need to find out how to overcome this.

0 comments:

Post a Comment