{"flag":true,"single":true,"pageTitle":"Multi-threading using curl php","post":{"id":308,"user_id":"1","slug":"multi-threading-using-curl-php-htd6","title":"Multi-threading using curl php","body":"<pre class=\"language-markup\"><code>&lt;?php\r\n\/\/ Initialize individual curl handles\r\n$ch1 = curl_init(\"https:\/\/www.example.com\");\r\n$ch2 = curl_init(\"https:\/\/www.google.com\");\r\n\r\n\/\/ Set options (optional)\r\ncurl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);\r\ncurl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);\r\n\r\n\/\/ Initialize the multi handle\r\n$mh = curl_multi_init();\r\n\r\n\/\/ Add the handles\r\ncurl_multi_add_handle($mh, $ch1);\r\ncurl_multi_add_handle($mh, $ch2);\r\n\r\n\/\/ Execute all requests\r\n$running = null; \r\ndo { \r\n    curl_multi_exec($mh, $running); \/\/runs as often as needed to move data forward, also it will update running to 2 because 2 process need to execute\r\n    curl_multi_select($mh); \/\/ wait for activity def 1 second,  (reduces CPU usage) &gt;ie Incoming data is ready to be read, Outgoing data can be sent, A socket state changes (connection established, closed, error, etc.)\r\n} while ($running &gt; 0);\r\n\r\n\/\/ Get the content\r\n$response1 = curl_multi_getcontent($ch1);\r\n$response2 = curl_multi_getcontent($ch2);\r\n\r\n\/\/ Close handles\r\ncurl_multi_remove_handle($mh, $ch1);\r\ncurl_multi_remove_handle($mh, $ch2);\r\ncurl_multi_close($mh);\r\n\r\ncurl_close($ch1);\r\ncurl_close($ch2);\r\n\r\n\/\/ Output results\r\necho \"Example.com:\\n$response1\\n\";\r\necho \"Google.com:\\n$response2\\n\";\r\n<\/code><\/pre>\r\n<p>&nbsp;<\/p>","category_id":"1","is_private":"0","created_at":"2025-08-22T02:16:55.000000Z","updated_at":"2025-08-22T02:16:55.000000Z","category":{"id":1,"user_id":"1","name":"PHP","slug":"php-3ius","parent_id":null,"created_at":"2023-03-14T03:58:19.000000Z","updated_at":"2023-03-14T03:58:19.000000Z"},"user":{"id":1,"name":"R GONDAL","email":"rizikmw@gmail.com","email_verified_at":null,"two_factor_confirmed_at":null,"current_team_id":"1","profile_photo_path":null,"created_at":"2023-03-12T10:49:33.000000Z","updated_at":"2025-01-10T12:59:00.000000Z","profile_photo_url":"https:\/\/ui-avatars.com\/api\/?name=R+G&color=7F9CF5&background=EBF4FF"}},"pageDesc":"&lt;?php \/\/ Initialize individual curl handles $ch1 = curl_init(\"https:\/\/www.example.com\"); $ch2 = curl_init(\"https:\/\/www.google.com\");  \/\/  - Multi-threading using curl php (Updated: August 22, 2025) - Read more about Multi-threading using curl php at my programming site [SITE]","categories":[]}