core error

cURL in PHP

Posted on: 12/08/2011


What?

cURL is a library of calls included in PHP core which allows to fetch data from remote sites. cURL is much safer than doing include or read_file on remote sites.

Why?

cURL is specifically designed for this function so you can do much more things than what you could do with include or read_file. For example, you can add specific HTTP options like timeouts and select th HTTP method (GET or POST).

How?

<?php

$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,'http://example.com');
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);

if (empty($buffer)){
    print "<p>Sorry, example.com is down at the moment.</p>";
}else{
    print $buffer;
}
?>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


    • Justin Cooney: Great comic, this made me laugh!
    • Justin Cooney: Interesting, I didn't know about the Google App Engine, I'm glad I found your article on it!

    Categories

    %d bloggers like this: