Dynadot

PHP to print the data.txt

Spaceship Spaceship
Watch
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
0
•••
You can use file_get_contents() function,

Code:
$file = 'http://livedraw.damacai.com.my/data/data.txt';
$content = file_get_contents($file);
$content = json_decode($content);

// for testing purpose
// print_r($content);
// var_dump($content);

// to get the data
echo 'ID: ' . $content->id;
echo 'Status: ' . $content->status;
 
1
•••
Enjoy:

Code:
function get_data($url) {
	$ch = curl_init();
	$header[] = "Accept: text/xml,application/xml,application/xhtml+xml,";
	$header[] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
	$header[] = "Cache-Control: max-age=0";
	$header[] = "Connection: keep-alive";
	$header[] = "Keep-Alive: 300";
	$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
	$header[] = "Accept-Language: en-us,en;q=0.5";
	$header[] = "Pragma: ";
	curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
	curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1510.64 Safari/537.31');
	curl_setopt($ch, CURLOPT_URL,$url);
	curl_setopt($ch, CURLOPT_FAILONERROR, true);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
	curl_setopt($ch, CURLOPT_AUTOREFERER, true);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
	curl_setopt($ch, CURLOPT_TIMEOUT, 10);
	curl_setopt($ch, CURLOPT_REFERER, 'http://google.com/');
	$data= curl_exec($ch);
	if (!$data) {
		return false;
	}
	curl_close($ch);
	return $data;
}


or to put into a variable:



Code:
if (!function_exists('getFileContents')) {
	function getFileContents($filename) {
		if (is_file($filename)) {
			ob_start();
			require($filename);
			$contents = ob_get_contents();
			ob_end_clean();
			return $contents;
		} else {
			return false;
		}
	}
}
 
0
•••
file_get_contents works fine! Test
Code:
<?php
$data = file_get_contents('http://livedraw.damacai.com.my/data/data.txt');
//do what you want with $data
?>
 
0
•••
You can use both file_get_contents or curl but file_get_contents() is great for simple GET requests as OP just want to read data from an url where the header, HTTP request method, timeout, cookiejar, redirects, and other important things does't matter.
 
0
•••
file_get_contents works fine! Test
Code:
<?php
$data = file_get_contents('http://livedraw.damacai.com.my/data/data.txt');
//do what you want with $data
?>

It isn't allowed on a lot of servers, based on the person setting it up. A lot of sites will block your request for various reasons. It's best not to get set in relying on file_get_contents();
 
0
•••
Yes file_get_contents() function is the easiest and fastest solution
 
Last edited:
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back