PHP:cURL #1

Keeper
11 years ago

0

Definion:

Sometimes when the developer feels the need of grabbing some external content, he would most usually refer to the file_get_contents() function. However, for more complex and sophisticated processes of extracting the client-side code from a page, one might stumble upon using cURL. cURL is a set of functions most of which are used to manipulate resources on an external server.

For those who might struggle downloading it (because there are always such - no fence), just to make it clear:

apt-get install php5-curl  

And of course to restart the Apache server:

apache2ctl restart  

Under Windows, you need to browse to the php.ini file and uncomment the line about cURL and then enable it from the extension of PHP. First thing we need to do is initialize the cURL library with the function curl_init() and point to an address.

Basic Usage:

<?php  

$ch = curl_init('http://google.com');  

Whether you would define the link in the initialization function or the options for cURL, it makes no difference. Alternatively, we could do it like so:

<?php  

$ch = curl_init();  
curl_setopt($ch, CURLOPT_URL, 'http://google.com');  

Echoing out the variable that holds the initialization in the first example, we get a 301 status code for a moved page. Whenever cURL makes a request to google.com, it gets a 301 header. Defining an option for cURL’s request will automatically force it to send the same request to the address that is return from the current/previous one. We do that the following way:

<?php  

$ch = curl_init('http://google.com');  
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);  
echo curl_exec($ch);  

We force cURL to follow the new location with the above option. This will return us the same result as file_get_contents() - we get the source code and partially the mark-up without the stylesheets. Now in most cases you wouldn’t want just to simply echo out the result. Suppose you were to append the supposed output of $ch to a variable. When doing that we ought to define a new option for cURL.

<?php  

$ch = curl_init('http://google.com');  
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
$output = curl_exec($ch)  
curl_close($ch);  
echo $output;  

Which forces the request received as an answer to be appended to that variable. Another thing to know is that many web pages check for the user-agent and as a whole the browser we’re using. Since we are using none with cURL, we need to define our own user-agent and define a header within it:

<?php  

$ch = curl_init('http://google.com');  
curl_setopt($ch, CURLOPT_HEADER, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)');  
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
$output = curl_exec($ch)  
curl_close($ch);  
echo $output;  

AJAX can pretty much fail this attempt because it is contained with the user browser and we cannot escape that with cURL. But that’s just food for thought, aside from the stuff we’re talking about. Now after we made the false impression that we’re using IE, let’s play a bit with the methods for sending the data. Let’s sent the request using $_POST:

<?php  
$Keeper = array('name' => 'Antagonism', 'Infamous' => '@/home/Keeper/example.php');  
$ch = curl_init('http://google.com');  
curl_setopt($ch, CURLOPT_POST, 1);  
curl_setopt($ch, CURLOPT_HEADER, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)');  
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
$output = curl_exec($ch)  
curl_close($ch);  
echo $output;  

This array will simulate an HTML form with an input type of text for the name element and one file field from which a user has decided to upload a .php file.

Conclusion:

cURL is pretty much self-explanatory. All function are actual objects and their names correspond to their functionality. Once you perceive how to initiate, define and close a connection, everything else can be found and easily accessed in the documentation of cURL in php.net. Questions or issues with the examples in this tutorial should be posted below. I’ll try to troubleshoot if anyone experiences difficulties with it. Thanks for reading!

0replies
1voice
266views
You must be logged in to reply to this discussion. Login
1 of 1

This site only uses cookies that are essential for the functionality of this website. Cookies are not used for tracking or marketing purposes.

By using our site, you acknowledge that you have read and understand our Privacy Policy, and Terms of Service.

Dismiss