I have been searching and trying a lot to find the answer to this problem, including trying out many answers to very similar questions asked here on StackOverflow that, as I read, worked really well for many users, however not for me, for reasons I don't know yet but hope to find the answer here.
My problem is that for some reason it turns out to be impossible for me to read the contents of any Crypto-JS from the GoogleCode pages in PHP (like http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha512.js). I started with the basic file_get_contents
and when that failed searched for other options on the web, which resulted in the following basic class:
<?php
include 'snoopy.class.php';
class StreamReader {
public static $agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)";
static $proxy = "http://proxy.whoisaaronbrown.com/proxy/";
public static function curlcontents($path) {
if(!function_exists('curl_version')) { return false; }
$handle = curl_init();
if(!$handle) { return false; }
$timeout = 30;
curl_setopt($handle, CURLOPT_URL, $path);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_VERBOSE, true);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($handle, CURLOPT_USERAGENT, StreamReader::$agent);
$lines_string = curl_exec($handle);
curl_close($handle);
return $lines_string;
}
public static function sockcontents($path, $port = 80) {
$handle = fsockopen($path, $port, $errno, $errstr, 30);
if (!$handle) {
echo " /* <b>$errstr ($errno)</b> */ "; return false;
} else {
$lines_string = "";
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: $path\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($handle, $out);
$data = false;
while (!feof($handle)) {
$data = fgets($handle, 128);
if($data === false) { break; }
$lines_string .= $data;
}
fclose($handle);
if($lines_string == "" && $data === false) { return false; }
return $lines_string;
}
}
public static function readcontents($path) {
if(!ini_get('allow_url_fopen')) { return false; }
// fopen opens webpage in Binary
// $handle = fopen($path,"rb");
$handle = fopen($path,"r");
if(!$handle) { return false; }
$lines_string = "";
$data = fread($handle,1024);
do {
if($data === false || strlen($data) == 0) {
break;
}
$lines_string .= $data;
$data = fread($handle,1024);
} while(true);
fclose($handle);
if($lines_string == "" && $data === false) { return false; }
return $lines_string;
}
public static function browsecontents($path) {
$snoopy = new Snoopy();
if($snoopy->fetch($path)) { return $snoopy->results; }
else { echo " /* <b>error fetching document: " . $snoopy->error . "</b> */ "; return false; }
}
}
?>
As you can see I copied the answers to "website - How to read a web page in PHP", "php - file_get_contents() failed to open stream" & "php - Why file_get_contents() returns "failed to open stream: HTTP request failed!"?", "fsockopen - PHP connect to external ip with port" and other optional solutions found here on StackOverflow as well as from SuperUser, PHP Freaks and others I found on the web.
(On a side note, since I don't have enough credits or something yet, SO don't allow me to post more than 2 links and so thanks to user bmla for de-uglifying this post)
My code is on an external host that I have no control over, but function_exists('curl_version')
returns true, as well as ini_get('allow_url_fopen')
does, however http_get
gives an undefined function error.
I have tried all functions on itself, as well as in combination with each other and with other techniques I found or could think of, among which is the redirection through Aaron Brown's proxy ($trythispathforachange = $proxy . $path;
). Sadly, all functions and combinations I have tried result in either a "connection failed", or a "connection timed out" error and in the case of Snoopy it tells me that the tcp://
-connection went sour. Also, for https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js everything works flawlessly, so I think the code must be correct so far.
But apparently for googlecode.com there still is something that prevents it from yielding a response to me in contrast to any human-interface-based browser.
Thanks in advance for any help or information provided!
Klaas