Can you share the full code? $fp is never set.
PHP CURL - File Download slow for some reason
Has anyone noticed their PHP CURL being slow to download remote files (jpg,css,js)? When I do wget on the server I get it in like 200ms.. when I do the same thing with CURL I get it in 800ms (isolated the CURL as issue).
Or do you have any suggestions about different approach to make the downloads faster? :) thank you!
$ch = curl_init();
curl_setopt( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 9.0; InfoChannel RNSafeBrowser/v.1.1.0G)' );
curl_setopt( $ch, CURLOPT_URL, $file_url );
curl_setopt( $ch, CURLOPT_DNS_SERVERS, "8.8.8.8,1.1.1.1" );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Expect:' ) );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_POST, false );
curl_setopt( $ch, CURLINFO_HEADER_OUT, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_ENCODING, 'gzip,deflate' );
curl_setopt( $ch, CURLOPT_BINARYTRANSFER, true );
curl_setopt( $ch, CURLOPT_HTTPGET, false );
curl_setopt( $ch, CURLOPT_NOBODY, false );
$refferer = parse_url( $file_url );
$refferer = $refferer['scheme'] . '://' . $refferer['host'];
if ( ! empty( $refferer ) ) {
curl_setopt( $ch, CURLOPT_REFERER, $refferer );
}
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_POSTREDIR, 7 );
curl_setopt( $ch, CURLOPT_FILE, $fp );
curl_setopt( $ch, CURLOPT_HEADER, 0 );
curl_setopt( $ch, CURLOPT_TIMEOUT, 60 );
#curl_setopt( $ch, CURLOPT_TCP_FASTOPEN , 1 );
#curl_setopt( $ch, CURLOPT_IPRESOLVE , CURL_IPRESOLVE_V4 );
$data = curl_exec( $ch );
$info = curl_getinfo( $ch );
curl_close( $ch );
fclose( $fp );
@hrvojek I have never seen anyone manually doing something like that. Perhaps you can use laravel octane, to reuse the bootstrapping of laravel, and then register the curl handler as a singleton. I haven't tried doing this myself, so I cannot say if there will be side effects.
Or you could perhaps dispatch it to a job running on the queue, to do it in the background.
Please or to participate in this conversation.