-
-
Save aalfiann/a4697a58f8389f330bd6e9e77b69598e to your computer and use it in GitHub Desktop.
PHP CURL Code to grab a file through a proxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
print get_file('http://www.google.com'); | |
function get_file($url) { | |
$curl = curl_init(); | |
$options = default_ops(array(CURLOPT_URL => $url)); | |
curl_setopt_array($curl, $options); | |
$output = curl_exec($curl); | |
//print $output; | |
$err = curl_errno($curl); | |
$errmsg = curl_error($curl); | |
if ( $err != 0 ) { | |
print "ERROR MESSAGE=" . $err . " $errmsg"; | |
} | |
$info = curl_getinfo($curl); | |
if ( $info['http_code'] != 200 ) { | |
throw new Exception('HTTP Error retrieving URL ' . $url . ' HTTP return code=' . $info['http_code'] . ' ' . $output); | |
} | |
return $output; | |
} | |
function default_ops($options = array()) { | |
$cookie_file = 'c:/temp/cookies.txt'; | |
$opts = array( | |
CURLOPT_CONNECTTIMEOUT => 30, /*seconds*/ | |
CURLOPT_TIMEOUT => 60, | |
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.17) Gecko/20110420 Firefox/3.6.17', | |
CURLOPT_HTTPHEADER => array("Expect:"), | |
CURLOPT_HEADER => false, // return headers | |
CURLOPT_FOLLOWLOCATION => false, // follow redirects | |
CURLOPT_ENCODING => "", // handle all encodings | |
CURLOPT_SSL_VERIFYHOST => false, // http://ademar.name/blog/2006/04/curl-ssl-certificate-problem-v.html | |
CURLOPT_SSL_VERIFYPEER => false, | |
// CURLOPT_VERBOSE => true, | |
CURLOPT_SSLVERSION => 3, | |
// CURLOPT_COOKIEFILE => $cookie_file, | |
// CURLOPT_COOKIEJAR => $cookie_file, | |
CURLOPT_RETURNTRANSFER => true, | |
// CURLOPT_HEADERFUNCTION => 'readHeader', | |
// CURLOPT_USERPWD => 'siteusername:sitepassword', | |
CURLOPT_HTTPPROXYTUNNEL=> true, | |
CURLOPT_PROXY => 'http://someproxy.com', | |
CURLOPT_PROXYPORT => 880, | |
CURLOPT_PROXYTYPE => CURLPROXY_HTTP, | |
CURLOPT_PROXYAUTH => CURLAUTH_ANY, | |
CURLOPT_PROXYUSERPWD => sprintf('%s:%s', 'proxyusername', 'proxypassword'), | |
); | |
foreach($options as $key=>$value) { | |
$opts[$key] = $value; | |
} | |
return $opts; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment