Using linkbucks API / posting data using cURL in PHP

Background:

Solution:

  • Register to linkbucks if you don’t have any account there (it’s free and you probably should use email other than gmail and hotmail if you want quick confirmation from linkbucks)
  • Find your username and API password from your profile page: https://www.linkbucks.com/Profile/ . If your API password is empty, just click “Reset API Password” link to show it.

This is our little PHP function that help us to POST data to certain URL:

//function to POST data using cURL in PHP
function curl_post($url, $data, $ip="", $ref="", $ua="") {
    //initiate cURL
    $ch=curl_init($url);
    
    //set referer only if stated by user
    if ($ref!="") {
        curl_setopt($ch, CURLOPT_REFERER, $ref);
    }
    
    //set IP interface if needed (for multi-IP server)
    if ($ip!="") {
        curl_setopt($ch, CURLOPT_INTERFACE, $ip);
    }
    
    //set user agent if stated by user
    if ($ua!="") {
        curl_setopt($ch, CURLOPT_USERAGENT, $ua);
    }
    
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    $response=curl_exec($ch);
    return $response;
}

How to use it to call linkbucks API:

$data_kirim=array(
    'user'=>'chmdznr',  //my username
    'apiPassword'=>'d55ae4eaf97b445f',  //my API password
    "originalLink"=>"http://google.com",  //the URL that need to be shorted
    "adType"=>2,  //paid links
    "contentType"=>1,   //not an adult content
    "domain"=>"yyv.co"  //target domain, a lot of possibility here, the default is: linkbucks.com 
);

//call linkbucks API
$url='https://www.linkbucks.com/api/createLink/single';

$returned_data=curl_post($url, json_encode($data_kirim));
$parsed_data=json_decode($returned_data);

//simple error checking
if (!empty($parsed_data->link)) {
    echo "This is your short URL : ".$parsed_data->link;
} else {
    echo "Something wrong! ".$parsed_data->errorDescription;
}

The curl_post function is quite generic, you can use it to send data to remote URL using POST method like this:

$login_data=array(
   'username'=>'admin',
   'password'=>'admin'
);

$target_url="http://the_login_website.com/";
$returned_data=curl_post($target_url, http_build_query($login_data));

You should notice that the data array need to be converted using http_build_query first (in linkbucks’ case, it doesn’t need to be converted first).
You can also add more advanced feature in the function such as handling the timeout and cookie.

  1. thank you

  2. Great, as usual waiting your review about it 🙂

  3. Thanks, works perfectly!!!

  4. If I want to call this script in bash linux. What should I do?

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>