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.

Let’s begin with an example to understand curl functionality practically.

cURL basic example

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.

Get title of page using cURL

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.

Information about cURL 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 servercurl_setopt($ch,CURLOPT_CONNECTTIMEOUT , 10 );

– maximum amount of time in seconds to which the execution of individual cURL extension function calls will be limitedcurl_setopt($ch,CURLOPT_TIMEOUT , 55 );

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.

15 Responses

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.

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!

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.

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!