unable to get webpage contents of a URL in PHP
Posted: Fri May 06, 2016 3:18 pm
Hi,
I am trying to open a webpage and get it's contents. On the browser, it works fine. However, through PHP, I'm unable to get webpage contents. In fact, I'm not able to hit the URL either. I feel anything apart from a header is not able to discover the webpage. With other URLs, it works fine. Please suggest me a method to able to get the contents of this webpage.
My code attempts
1. fopen
2. Using curl
PS : In all of the above methods, I also tried prepending the URL with compress.zlib since it initially gave me encrypted characters. So the URL would look like the following. Still no luck.
PS : I had posted a similar question on another forum, however, I wasn't sure whether it was published due to site problems, and am not tracking it. My intention is not to cross post 
I am trying to open a webpage and get it's contents. On the browser, it works fine. However, through PHP, I'm unable to get webpage contents. In fact, I'm not able to hit the URL either. I feel anything apart from a header is not able to discover the webpage. With other URLs, it works fine. Please suggest me a method to able to get the contents of this webpage.
Code: Select all
URL : https://kat.cr/usearch/life%20of%20pi/1. fopen
Code: Select all
$url = "https://kat.cr/usearch/life%20of%20pi/";
$handle = fopen($url, "r");Code: Select all
/* gets the data from a URL */
function get_data($url) {
$userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$url = "https://kat.cr/usearch/life%20of%20pi/";
$returned_content = get_data($url);
echo $returned_content;
}Code: Select all
$url = "compress.zlib://https://kat.cr/usearch/life%20of%20pi/";