This article is the first part of the series using curl in php, if you know the basic you can visit Sending post request using curl.
cURL
It is a library that helps to receive or transfer data using protocols like DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP.
cURL in PHP
It is a library used to make HTTP request in PHP.In layman terms, cURL gives the same effect as a user opening a web page in the browser after disabling javascript.When a HTTP request is made in php using curl then the received content contains the source code of the page(after disabling javascript).cURL is a resource, a resource in php is a reference to a data hold by some other module.
Installation of cURL in php can be seen at php.net
Let’s begin with an example to understand curl functionality practically.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php // url for sending http request $url = "http://tusharsharma.in"; // this will initalize a curl session $ch = curl_init(); // set url for http request curl_setopt($ch, CURLOPT_URL,$url); // used to get request output as string curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); // execute the request $output = curl_exec($ch); //end of curl request curl_close($ch); ?> |
Variable $output contains the source code of the web page http://tusharsharma.in. Now let’s take out title of the page using regular expression.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php // url for sending http request $url = "http://tusharsharma.in"; // this will initalize a curl session $ch = curl_init(); // Better way of setting curl options $curl_options = array( CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true ); // setting curl option, all at once curl_setopt_array($ch, $curl_options); // execute the request $output = curl_exec($ch); // detailed info about our curl request $curl_info = curl_getinfo($ch); //end of curl request curl_close($ch); $title = preg_match('/<title[^>]*>(.*?)<\/title>/ims', $output, $matches) ? $matches[1] : "No title found"; var_dump($title); ?> |
Above code outputs string(45) “Tushar Sharma | Technophile and PHP developer”.Here we take output as a string and pass in the preg_match and get title of the page.
To get information about any curl request we use a function curl_getinfo(curl resource). This function gives output as follows which contains information about content/type, http code, header size, port, content length and several other parameters related to our request.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
Array ( [url] => http://tusharsharma.in/ [content_type] => text/html [http_code] => 200 [header_size] => 158 [request_size] => 54 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.374 [namelookup_time] => 0.016 [connect_time] => 0.187 [pretransfer_time] => 0.187 [size_upload] => 0 [size_download] => 7410 [speed_download] => 19812 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => 0 [starttransfer_time] => 0.374 [redirect_time] => 0 [redirect_url] => [primary_ip] => 160.153.16.12 [certinfo] => Array ( ) [primary_port] => 80 [local_ip] => 192.168.1.4 [local_port] => 50408 ) |
Some important cURL options are as follows:
– maximum time required to make a connection to a server
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT , 10 );
– maximum amount of time in seconds to which the execution of individual cURL extension function calls will be limited
curl_setopt($ch,CURLOPT_TIMEOUT , 55 );
– follow url after redirection
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
– verify the certificate’s name against host
curl_setopt($ch , CURLOPT_SSL_VERIFYHOST , true);
– verify the peer’s SSL certificate
curl_setopt($ch , CURLOPT_SSL_VERIFYPEER , true);
– set useragent of the browser to check how webpage looks in that particular browser
curl_setopt($ch, CURLOPT_USERAGENT , Mozilla/5.0 (Windows NT 6.1; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0 );
– file name to store cookies to
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_location);
– file name to read cookies from
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_location);
Error Handling
Mostly curl_error(curl resource)
and curl_errorno(curl resource)
are used to get error information during a curl session.curl_error returns an error in the form of string and curl_errorno return error code.Also you can look for http code if you got any error.In case the request fails, http code could be 404, 503 or anything else but not 200.Check complete list of http code at Wikipedia.
cURL is a very useful library in php which can perform some cool stuff if used wisely.Hope you enjoy the tutorial and tell me what you have done using curl in php.
Hello There. I found your blog the usage of msn. That is a really neatly written article.
I’ll make sure to bookmark it and return to learn more of
your helpful info. Thanks for the post. I will certainly
return.
Howdy! This article could not be written much better!
Looking at this article reminds me of my previous roommate!
He continually kept talking about this. I most certainly will send this article
to him. Fairly certain he’s going to have a great read.
I appreciate you for sharing!
Using cURL with Proxies Network https://p2proxy.wordpress.com/2015/11/21/p2p-proxy-with-curl/
I’m not that much of a internet reader to be honest but your blogs really nice, keep
it up! I’ll go ahead and bookmark your website to come back later
on. All the best
I used to be recommended this web site by way of my cousin. I am not certain whether this submit is written via him as nobody
else recognize such targeted approximately my problem.
You’re amazing! Thanks!
I read this article completely about the difference of newest
and preceding technologies, it’s amazing article.
This is my first time pay a visit at here and i am really
happy to read all at one place.
Hmm it looks like your website ate my first comment (it was super long) so I
guess I’ll just sum it up what I wrote and say, I’m
thoroughly enjoying your blog. I too am an aspiring blog writer but I’m still new to the whole thing.
Do you have any points for first-time blog writers?
I’d really appreciate it.
Just one advice “Write what your user wants.”
Thanks designed for sharing such a fastidious thinking, post is
pleasant, thats why i have read it entirely
You actually make it seem so easy with your presentation but I
find this topic to be really something which I think I would
never understand. It seems too complicated and extremely broad for me.
I am looking forward for your next post, I will try to get the
hang of it!
magnificent post, very informative. I’m wondering why the opposite specialists of this sector do not understand this.
You should proceed your writing. I am confident, you have
a huge readers’ base already!
I’ve read several just right stuff here. Definitely price
bookmarking for revisiting. I surprise how much effort you put to create such a
wonderful informative website.
What a information of un-ambiguity and preserveness of precious know-how concerning unpredicted feelings.
I like the helpful information you provide on your
articles. I’ll bookmark your blog and take a look at once more right here regularly.
I’m reasonably certain I’ll learn lots of
new stuff right right here! Good luck for the next!