Salut
Probabil ca acel server are blocat accesul la pagini externe cu file_get_contents(). Inceara direct cu simplexml_load_file():
Cod: Selectaţi tot
$xml = simplexml_load_file('Adresa_Pagina');
print_r($xml);
Sau, indicat cu cURL.
Pe localhost, cu XAMPP, a functionat.
Cod: Selectaţi tot
// cUrl to get content from external page ( https://coursesweb.net/ )
function getPage($url) {
// Browser agents
$agents = array('Mozilla/5.0 (Windows NT 6.1; rv:21.0) Gecko/20100101 Firefox/21.0', 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; WOW64; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; Media Center PC 5.0)', 'Opera/9.63 (Windows NT 6.0; U; ru) Presto/2.1.1', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5');
// adds headers data
$curl_header = array('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0', 'Connection: keep-alive', 'Keep-Alive: 84600', 'Content-Type: text/html;charset=UTF-8');
if($ch = curl_init()) {
curl_setopt($ch, CURLOPT_USERAGENT, array_rand($agents, 1));
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $curl_header);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
curl_setopt($ch, CURLOPT_TIMEOUT, 84600);
curl_setopt($ch, CURLOPT_NOSIGNAL, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_URL, $url);
$str = curl_exec($ch);
curl_close($ch);
return utf8_decode($str);
}
else return 'Unable to use CURL on this server';
}
$feed = getPage('Adresa_Pagina');
$xml = simplexml_load_string($feed);
print_r($xml);