Senin, 14 Februari 2011

Things You Can Do With PHP cURL

cURL, and its PHP extension libcURL, are tools which can be used to simulate a web browser. And there are lots you can do with PHP Curl, here are some of them:
1. Download a file
You can download a file using PHP cURL, and it will download using HTTP request:
set_time_limit(0); // set no time limit to download large file
ini_set('display_errors',true);//Just in case we get some errors, let us know....
$fp = fopen ('path to file', 'w+');//where the file will be saved
$ch = curl_init('');//Here is the file we are downloading
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
2. Upload with HTTP or FTP protocol
You can upload a file using PHP cURL, and it will upload using HTTP or FTP:
Upload file using HTTP:
$postdata = array();
$postdata ['fieldname'] = "@/path/to/";  //fieldname should be same as file input box name
$post_url = ''; //url to upload file
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$response = curl_exec($ch);
curl_close ($ch);
Upload file using FTP:
$filepath = '/path/to/local/';
$fp = $fp = fopen($filepath, 'r'); //open as read a file
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ''.'');
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filepath));
curl_exec ($ch);
$error_no = curl_errno($ch);
curl_close ($ch);
3. Shorten a URL using is an URL shortener service, and it allow us to shorten url using their API. And we can connect to their API using PHP Curl:
$login='your username';
$appkey='your app key';
$api_url = ''.urlencode($longurl).'&format=xml&login='.$login.'&apiKey='.$appkey;
//call the API
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $api_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_ex_ec($curl);
//parse the XML response and return the url
$xml_object = new SimpleXMLElement($response);
echo $xml_object->results->nodeKeyVal->shortUrl; //output the shortened url
4. Publish post to your WordPress blog
Wordpress blog have XMLRPC feature to publish a post and publish a comment. We can use cURL to connect to this WordPress blog XMLRPC and publish a post. See my previous post: [HowTo] Publish Post Via XML-RPC In WordPress.
5. Scrape the website content
Basically all the web content is in HTML. You can grap the website content and filter the content and grab the information you want to get. For example you can use cURL to grab the search result from Google, Bing, Yahoo and etc.
function getPage($proxy, $url, $referer, $agent, $header, $timeout) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, $header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_PROXY, $proxy);
    curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_REFERER, $referer);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    $result['EXE'] = curl_exec($ch);
    $result['INF'] = curl_getinfo($ch);
    $result['ERR'] = curl_error($ch);
    return $result;
$result = getPage(
    '[proxy IP]:[port]', // leave it blank of using no proxy
    'url to scrape start with http://',
    'referer url', // leave it blank if if no referer
    'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/2009032609 Firefox/3.0.8',
//Process the $result['EXE'] here
6. Update your Facebook status
With cURL we can connect to Facebook and simulate as it is using a web browser and publish a status.
$status = 'YOUR_STATUS';
$first_name = 'YOUR_FIRST_NAME';
$login_email = 'YOUR_LOGIN_EMAIL';
$login_pass = 'YOUR_PASSWORD';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ';');
curl_setopt($ch, CURLOPT_POSTFIELDS,'email='.urlencode($login_email).'&pass='.urlencode($login_pass).'&login=Login');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/20070309 Firefox/");
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, '');
$page = curl_exec($ch);
curl_setopt($ch, CURLOPT_POST, 1);
preg_match('/name="post_form_id" value="(.*)" \/>'.ucfirst($first_name).'/', $page, $form_id);
curl_setopt($ch, CURLOPT_POSTFIELDS,'post_form_id='.$form_id[1].'&status='.urlencode($status).'&update=Update');
curl_setopt($ch, CURLOPT_URL, '');
7. Test your download speed connection
cURL have information of the connection status. You can grab the information and show you the how long the download time and how fast your download speed.
<?php error_reporting(E_ALL | E_STRICT);
// Initialize cURL with given url
$url = '';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'Sitepoint Examples (thread 581410;');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
$execute = curl_exec($ch);
$info = curl_getinfo($ch);
// Time spent downloading, I think
$time = $info['total_time']
      - $info['namelookup_time']
      - $info['connect_time']
      - $info['pretransfer_time']
      - $info['starttransfer_time']
      - $info['redirect_time'];
// Echo friendly messages
header('Content-Type: text/plain');
printf("Downloaded %d bytes in %0.4f seconds.\n", $info['size_download'], $time);
printf("Which is %0.4f mbps\n", $info['size_download'] * 8 / $time / 1024 / 1024);
printf("CURL said %0.4f mbps\n", $info['speed_download'] * 8 / 1024 / 1024);
echo "\n\ncurl_getinfo() said:\n", str_repeat('-', 31 + strlen($url)), "\n";
foreach ($info as $label => $value)
 printf("%-30s %s\n", $label, $value);
8. Get your adsense earning
Most blogger monetize his website using adsense, including me. You can grab your adsense earning using cURL:
Download the script. (Save it as php file)
Source from:
9. Post to twitter
Twitter have their API, and we can use cURL to post our tweet to twitter through their API.
// Set username and password
$username = 'username';
$password = 'password';
// The message you want to send
$message = 'is twittering from php using curl';
// The twitter API address
$url = '';
// Alternative JSON version
// $url = '';
// Set up and execute the curl process
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "$url");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$message");
curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");
$buffer = curl_exec($curl_handle);
// check for success or failure
if (empty($buffer)) {
    echo 'message';
} else {
    echo 'success';
10. Download a video from YouTube
With cURL we can scrape the YouTube website to get the video url and download it.
Download the script here.

1 komentar:

  1. Did you know that you can create short links with Shortest and make money from every visit to your short urls.