public function replaceFromURL(UploadedFile $file, $url)
{
/* Some sites don't like automated requests. But the internet is meant to be open for anybody,
* even for scripts. So we are evil and fake the headers.
*
* Credit goes to Ryan Rampersad from whom I copied most code.
* http://blog.ryanrampersad.com/2008/11/07/get-remote-html-with-curl-and-php/
*/
$curl = curl_init();
$header[0] = 'Accept: text/xml,application/xml,application/xhtml+xml,';
$header[0] .= 'text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5';
$header[] = 'Cache-Control: max-age=0';
$header[] = 'Connection: keep-alive';
$header[] = 'Keep-Alive: 300';
$header[] = 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7';
$header[] = 'Accept-Language: en-us,en;q=0.5';
$header[] = 'Pragma: ';
$browser = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008092510 ';
$browser .= 'Ubuntu/8.04 (hardy) Firefox/3.0.3';
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_USERAGENT, $browser);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_MAXREDIRS, 7);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($curl);
if ($data === false) {
$curlError = curl_error($curl);
// Strip ANY tags from the error message. curl tends to spit out <url> is not valid, which then
// confuses the error message parser on the client side.
$curlError = str_replace(['>', '<'], '', $curlError);
throw new \Exception('replaceFromURL error: ' . $curlError);
}
curl_close($curl);
$this->replaceFromData($file, $data, basename($url));
}