Curl is a command line tools to transfer data using various protocol. Curl project was released with libcurl, which is a library for curl that can be use in multi programming language. But it is widely use for PHP with PHP Curl library.
“curl is a command line tool for transferring data with URL syntax, supporting DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, TELNET and TFTP. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, kerberos…), file transfer resume, proxy tunneling and a busload of other useful tricks. ” (Quote from Curl Official site).
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('http://somedomain.com/filename.zip');//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); curl_exec($ch); curl_close($ch); fclose($fp);
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/file.zip"; //fieldname should be same as file input box name $post_url = 'http://somedomain.com/upload.php'; //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($ch, 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/file.zip'; $fp = $fp = fopen($filepath, 'r'); //open as read a file $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'ftp://ftp_login:[email protected]/'.'filaname.zip'); 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 Bit.ly
Bit.ly 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:
$longurl='http://www.ivankristianto.com/'; $login='your bit.ly username'; $appkey='your app key'; $api_url = 'http://api.bit.ly/shorten?version=2.0.1&longUrl='.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); curl_close($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); curl_close($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:1.9.0.8) Gecko/2009032609 Firefox/3.0.8', 1, 5); //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.
<?php $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, 'https://login.facebook.com/login.php?m&next=http%3A%2F%2Fm.facebook.com%2Fhome.php'); 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:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3"); curl_exec($ch); curl_setopt($ch, CURLOPT_POST, 0); curl_setopt($ch, CURLOPT_URL, 'http://m.facebook.com/home.php'); $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, 'http://m.facebook.com/home.php'); curl_exec($ch); ?>
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 = 'http://download.bethere.co.uk/images/61859740_3c0c5dbc30_o.jpg'; $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_USERAGENT, 'Sitepoint Examples (thread 581410; http://www.sitepoint.com/forums/showthread.php?t=581410)'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2); curl_setopt($ch, CURLOPT_TIMEOUT, 60); set_time_limit(65); $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 mbpsn", $info['size_download'] * 8 / $time / 1024 / 1024); printf("CURL said %0.4f mbpsn", $info['speed_download'] * 8 / 1024 / 1024); echo "nncurl_getinfo() said:n", str_repeat('-', 31 + strlen($url)), "n"; foreach ($info as $label => $value) { printf("%-30s %sn", $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: http://planetozh.com/blog/my-projects/track-adsense-earnings-in-rss-feed/
9. Post to twitter
Twitter have their API, and we can use cURL to post our tweet to twitter through their API.
<?php // 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 = 'http://twitter.com/statuses/update.xml'; // Alternative JSON version // $url = 'http://twitter.com/statuses/update.json'; // 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); curl_close($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.
Posting on twitter using basic authentication as shown in example 9 is no longer supported
Any idea on how to pass data to telnet connection? I have searched and searched and not being a php guru I don't have a clue how to pass the data to stdin so its passed to the telnet server.
Love the examples above and will definitely learn something from them but if you have any idea how to help I would be truly grateful
If you need the tool which passes data use <a href="http://phpforms.net/">php forms</a>. It passes any kind of data and is simple in use.
Thanks Pavel, will look at it.
nice .. wether youtube downloader still working?
This is a nice article.
Hey, can u plz help me, How to pass list of servers in one Nagios connection through socket programming(LQL) ?
Thanks in advance.
not any of the above examples worked for me at all.. Is there something missing..
I do not get any errors when trying to curl, just nothing comes back. The facebook example does not update the status.
I have copied and pasted the code in your examples and only change the necessary information. Again, no errors, just nothing returned…
well you can debug it to see if the curl get the information you want to get.
http://www.ivankristianto.com/web-development/programming/tips-write-print_r-output-to-file/1461/
not any of the above examples worked for me at all.. Is there something missing..
I do not get any errors when trying to curl, just nothing comes back. The facebook example does not update the status.
I have copied and pasted the code in your examples and only change the necessary information. Again, no errors, just nothing returned…
How can I download facebook videos by URL. help me
can ve post on blogspot using Curl? if Yes then please post Code…
http://www.ivankristianto.com/internet/blogging/howto-publish-post-via-xml-rpc-in-wordpress/1428/
hello ivan i copied ur posting on twitter and run the program but the status is not updated why?
posting to twitter with basic auth is deprecated and no longer valid. you should use oAuth instead.
Hi ivan, i am newbie on using phpcurl and on new twitter oauth, can you please show me how to use the new oauth on posting a tweet on twitter/?
Thanks!
I will make that tutorial in near future. I have gather the sources and making the example. So stay tune!
Mistake in HTTP Upload section code "curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);" – you don't have $curl variable defined
@vtulin My mistake: it should be curl_setopt($chrl, CURLOPT_BINARYTRANSFER, 1);
i fixed it already. thanks.
Hi!I would like to know how I can upload a fil from my server to another server via CURL php.For example I have an image in http://www.example.com/image.jpg and I want it to upload to another server via CURL PHP.If this is not posible, I would like to know if I can do it in another way.Thanks
@sistemasweb Imagine curl as you browse the page but it is automate by the php script. So you cannot upload the file if they don't provide the upload interface. Otherwsie you can automate it from ftp connection.
Hi I m looking for a code. How to login to the sites like upload.com, hotfile.com, mediafire.com etc. using cURL i have a premium membership of these sites and also want to khow how to set the cookie path.
@kaustav hotfile support ftp upload, so you can use ftp connection. but for upload.com and mediafire.com you need to learn how they do POST when upload file. But i think it is hard to do since they have their own mechanism behind that. try to ask if they have an API.
thanks great article ! Do you know if can I restrict curl request just from specific IP address ?!
Yes you can. you can filter it from the script that respond your http request.