Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hrvojek's avatar

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 );
0 likes
5 replies
Sinnbeck's avatar

Can you share the full code? $fp is never set.

hrvojek's avatar

@Sinnbeck The code is massive :) because I do a lot of verifications before the CURL... Fp is just file open

$fp = fopen($this->params['path'], 'w+');

Basically I know the CURL is the problem because once the file is downloaded I can reuse it without curl and everything is much faster, the problem occurs when the API is in (for example) NYC while the remote file is in Sydney, that's understandable - but I'm downloading MANY files, but 1 HTTP request = 1 PHP Process (1 file) and the CURL handle is not reused as PHP new process is created.

So I'm starting to think... can I make a thread controller, so first HTTP request goes to Thread Controller , then thread controller figures out to which PHP thread should that request go and forwards it, is that something doable even? :) Does Laravel have something similar?

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@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.

hrvojek's avatar

@Sinnbeck I will first try to play with PHP alone.. and if it works there I will need to dig through Laravel to see if it has any similar idea... :) but from what I'm thinking about thread controller.. it's doable - logically.. I guess PHP as always will have some different idea than me :P

Thanks for your suggestions and help!

Please or to participate in this conversation.