Friday, November 14, 2014

How to Download a URL's Content Using PHP

Due to the highly increased number of websites and the usage of web services, sometimes we just need to download content from web using a URL. For PHP Programmer, There are two methods to achieve this goal.

Download URL Content

Download URL Content

First one by using the file_get_content() function and
Second one by using the curl() function in php

Some host doesn't allow file_get_content() function for some security reasons.

curl() function :-

$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);
print_r($data);

Or Another code is as below :-

file_get_content() function :-

file_get_contents('http://www.example.com/');

0 comments:

Post a Comment