Using cURL with PHP
PHP/MySQL
Last Updated: 2007-02-03 08:01:44
Last Updated: 2007-02-03 08:01:44
STEP 1: cURL
cURL stands for 'Client URL', and is perhaps the most powerful PHP extension available. cURL allows you to communicate with other servers. This may not sound like it's that interesting until you fully understand what you can do with it. For instance you can write internet crawlers that gather information for search engine results (HINT: see my search engine page). You can write merchant account gateways that validate credit card information with your bank in line with your script so there is no refreshing of the page. You can process information on FTP servers. You can gather stock information and weather information from other sites to use on your own. The possibilities are endless.
In this tutorial we are going to do something very basic. We are going to build an online dictionary using definitions from another site.
To use the cURL library you must have cURL installed as an extention to your PHP installation. If you are looking for a web host who already has cURL extentions installed, a good one that I use is fatcow.com. If you do use them be sure to list Joe Fitzgerald as your reference, because they will give me a free month of hosting if you do... and I'd appreciate that.
In this tutorial we are going to do something very basic. We are going to build an online dictionary using definitions from another site.
To use the cURL library you must have cURL installed as an extention to your PHP installation. If you are looking for a web host who already has cURL extentions installed, a good one that I use is fatcow.com. If you do use them be sure to list Joe Fitzgerald as your reference, because they will give me a free month of hosting if you do... and I'd appreciate that.
STEP 2: Submitting the Word
First we create the form users will use to input a word for defining.
<html>
<body>
<form action="getDefinition.php" method="POST">
Please enter a word:
<input type="text" name="word">
<input type="submit">
</form>
</body>
</html>
STEP 3: Initialize cURL
When the form we created in the previous step is submitted it posts to the getDefinition.php page.
This is the page we will put our cURL code on. Here we will initialize the cURL session.
<?php
// this code would go in getDefinition.php
//get the word submitted from the form
$word = addslashes($_POST['word']);
// initialize the cURL session
$ch = curl_init();
?>
This is the page we will put our cURL code on. Here we will initialize the cURL session.
<?php
// this code would go in getDefinition.php
//get the word submitted from the form
$word = addslashes($_POST['word']);
// initialize the cURL session
$ch = curl_init();
?>
STEP 4: Set cURL options
Now that our session is initialized we will set up some options for our cURL script using curl_setopt().
With the first option we are going to tell cURL to connect to dict.org with the dict protocol.
With the second option we are telling cURL to place the results from dict.org into a variable named $ch rather than printing the results to our browser.
<?php
// this code would go in getDefinition.php
//get the word submitted from the form
$word = addslashes($_POST['word']);
// initialize the cURL session
$ch = curl_init();
// Set the URL, which includes $word, and is of the dict protocol
curl_setopt($ch, CURLOPT_URL, "dict://dict.org/d:($word)");
// Return the output from the cURL session rather than displaying in the browser.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
?>
With the first option we are going to tell cURL to connect to dict.org with the dict protocol.
With the second option we are telling cURL to place the results from dict.org into a variable named $ch rather than printing the results to our browser.
<?php
// this code would go in getDefinition.php
//get the word submitted from the form
$word = addslashes($_POST['word']);
// initialize the cURL session
$ch = curl_init();
// Set the URL, which includes $word, and is of the dict protocol
curl_setopt($ch, CURLOPT_URL, "dict://dict.org/d:($word)");
// Return the output from the cURL session rather than displaying in the browser.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
?>
STEP 5: Executing the Script
Now all that remains is to execute the cURL script and display the results. We will also free up any system resources occupied by cURL by ending our session.
<?php
// this code would go in getDefinition.php
//get the word submitted from the form
$word = addslashes($_POST['word']);
// initialize the cURL session
$ch = curl_init();
// Set the URL, which includes $word, and is of the dict protocol
curl_setopt($ch, CURLOPT_URL, "dict://dict.org/d:($word)");
// Return the output from the cURL session rather than displaying in the browser.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//Execute the session, returning the results to $definition
$definition = curl_exec($ch);
// close your session and free up the resources taken up by cURL
curl_close($ch);
//display the results
print "$x: $definition";
?>
<?php
// this code would go in getDefinition.php
//get the word submitted from the form
$word = addslashes($_POST['word']);
// initialize the cURL session
$ch = curl_init();
// Set the URL, which includes $word, and is of the dict protocol
curl_setopt($ch, CURLOPT_URL, "dict://dict.org/d:($word)");
// Return the output from the cURL session rather than displaying in the browser.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//Execute the session, returning the results to $definition
$definition = curl_exec($ch);
// close your session and free up the resources taken up by cURL
curl_close($ch);
//display the results
print "$x: $definition";
?>
This is the JoeLucky39 tutorial section. Feel free to use whatever information you find here on your own site(s). Know that not all tutorials are ideal for all sites, so feel free to modify the information contained in this tutorail section as best suits your web site.
At JoeLucky39 you can find tutorials on Comic Art, PHP/MySQL, and more are added all the time. So, visit often and feel free to contact Joe if you think that his tutorials need to be changed or updated.
Joe believes in the open source movement and is happy to share his knowledge with anyone who asks. All Joe asks is, if you use Joe's open source tutorials, you share your knowledge as well.
Thank you and I hope you find what you are looking for. If not feel free to contact me and ask.
