Sunday, December 24, 2017

Download a URL’s Content Using PHP cURL

In this tutorial we are going to learn how to extract or download the content from the specific URL. Nowadays it is a common and useful technique to extract data from another server. By this way we can extract useful data like meta tag details, html attribute/ tags details.
PHP's cURL library, which often comes with default shared hosting configurations, allows web developers to complete this task.

Lets see the below function which will extract information from specific URL.
PHP cURL Method.

/* gets the data from a URL */
function get_data($url) {
 $ch = curl_init();
 $timeout = 5;
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
 $data = curl_exec($ch);
 curl_close($ch);
 return $data;
}

To call get_data() function, you need to mention below statement in PHP tag.
$returned_content = get_data('https://www.skptricks.com');

Alternative Method 

Alternatively, you can use the file_get_contents function remotely, but many hosts don't allow this.

<?php
$URL ="http://www.skptricks.com"
file_get_contents($URL)
?>




No comments:

Post a Comment