Forum Topic: Php: Intro To Curl

(798 views • 2 replies)

This topic is 1 page long.

<< < > >>
None

SpamBurger

Reply To Post Reply & Quote

Posted at: 10/1/06 11:05 AM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

Php: Main

In this tutorial, you will be learning about the basics of CURL. What is CURL you ask? CURL is a library used to communicate with other servers through various protocols. I will only be teaching you the most important things to know, but don't forget there is still more you can learn.
------------
Setting up CURL

Before you can start using the CURL library, you will need to download it. You can find it here. Once you have it set up, you can move on.
------------
Starting a CURL Session

Before using CURL, we need to initialize it. To do this, we use the curl_init() function. This is how it is done:

<?php
$cs=curl_init();
?>

We store the session in a variable so it can be accessed later. The curl_init() function also has the optional url parameter, so we can specify which url to work with right when we start the session. This saves time because you don't need to do curl_setopt($cs,CURLOPT_URL,'http://exampl
e.com
')
. An example with the optional parameter:

<?php
$cs=curl_init('http://example.com');
?>

------------
Setting Options for the CURL Session

Once we have the CURL session initialized, we need to start setting options for the session. To do this, we use the curl_setopt() function which takes three parameters. The first parameter is the CURL handler, the second is which option we are setting, and the third is the value of the option we are setting. Note that you can only set one option at a time, but that is fine because you can just call the function multiple times. Anyways, say we didn't set the url we will be working with in the handler. We can do that in the curl_setopt() function like this:

<?php
$cs=curl_init();
curl_setopt($cs,CURLOPT_URL,'http://exampl
e.com
');
?>

Now, we can start working with example.com. Below is a list of common options you will be using, and what they do:

CURLOPT_URL- Sets the url you will be communicating with.
CURLOPT_POST- Set to true if you want to send POST data.
CURLOPT_RETURNTRANSFER- If set to true, a call to curl_exec() will return the value instead of outputting it.
CURLOPT_URL- Sets the url you will be communicating with.
CURLOPT_POSTFIELDS- If sending data via POST, use this to set the data to send. Each variable should be separated by an ampersand (&) like this: name=foo&message=bar
CURLOPT_HTTPHEADER- An array of headers to be sent.

Those are all the basic options you will be using. However, there are many, many more. For a full list check here.
------------
Executing a CURL Session

Once you have all your CURL options set, it is time to execute the CURL session. To do this, use the curl_exec() function which takes one parameter, the handler of the CURL session (in our case, $cs). Remember that if you did not set CURLOPT_RETURNTRANSFER to true, this function will automatically output the contents from the page instead of returning its value. Example:

<?php
$cs=curl_init();
curl_setopt($cs,CURLOPT_URL,'http://exampl
e.com
');
curl_exec($ch); //Outputs the contents of example.com
?>

If you want to store the contents in a variable, do this:

<?php
$cs=curl_init();
curl_setopt($cs,CURLOPT_URL,'http://exampl
e.com
');
curl_setopt($cs,CURLOPT_RETURNTRANSFER,1);
$data=curl_exec($ch); //$data holds the value of example.com's contents
?>

Pretty simple, eh?
------------
Closing a CURL Session

To close a CURL session and free up resources, use the curl_close() function which takes one parameter, the session handler. I don't think this really needs an example because it is pretty self explanatory.
------------
Tutorial Over

Well, that is my tutorial. I hope you learned the basics of CURL. If you want to see a script where CURL is used, check this tutorial.

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

JeremysFilms

Reply To Post Reply & Quote

Posted at: 10/1/06 11:12 AM

JeremysFilms NEUTRAL LEVEL 17

Sign-Up: 02/18/05

Posts: 1,522

thank you!!


None

SpamBurger

Reply To Post Reply & Quote

Posted at: 10/1/06 02:53 PM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

Thanks for the comments :)

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


All times are Eastern Standard Time (GMT -5) | Current Time: 08:53 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!