Skip to content

Instantly share code, notes, and snippets.

@mlconnor
Created March 14, 2012 16:21
Show Gist options
  • Save mlconnor/2037612 to your computer and use it in GitHub Desktop.
Save mlconnor/2037612 to your computer and use it in GitHub Desktop.
PHP CURL Code to grab a file through a proxy
<?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