View Tutorial Metadata Edit Content Revision History Add to Watchlist Add New Tutorial Read a remote file using CURL in PHP

Read a remote file using CURL

Read a remote file using CURL

On some hosts the only way to read a remote file is using curl because they forbids PHP from reading non-local resources. In order to get past their limitation you need to use a small trick .. use the CURL library.

To check if your server has CURL, see Checking if your system has a PHP extension installed and check if cURL support is enabled.

The function needs to be called by providing the URL for the resource you need and it will retrieve the resource's content.

Example of usage :

$content = getFile('http://codepax.com');

and the function itself :

function getFile($url){
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$tmp = curl_exec($ch);
	curl_close($ch);
	if ($tmp != false){
		return $tmp;
	}
}

Only plain text supported.

Optional

Required - will be kept private

Optional

 
 

Rating: (0+, 0-) In: PHP 5