- Impact
- 49
Anyone knows how to use PHP to printout the contents of data.txt from the url below?
http://livedraw.damacai.com.my/data/data.txt
http://livedraw.damacai.com.my/data/data.txt
$contents = curl_exec($ch);
$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;
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;
}
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;
}
}
}
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 ?>