V
V
Viktor2020-04-08 09:58:28
PHP
Viktor, 2020-04-08 09:58:28

How to send multithreaded data via cURL?

I need to send more than one stream at the same time through cURL data to the server. I made one thread and it works fine, but I can no longer understand how to organize it.
I did it in one thread and it works.

$ch = curl_init($url);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(	
    'Content-Type: application/rss+xml',
    'Authorization: OAuth ' . TOKEN
   	));
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   	curl_setopt($ch, CURLOPT_POSTFIELDS, $putData); 
  curl_close($ch);


I did this multithreaded, but it seems to me that it does not work as it should
foreach ($files as $i => $data) {
    $curl_arr[$i] = curl_init($url);
    curl_setopt($curl_arr[$i], CURLOPT_HTTPHEADER, array(
            'Content-Type: application/rss+xml',
            'Authorization: OAuth ' . TOKEN
        ));
    curl_setopt($curl_arr[$i], CURLOPT_POST, 1);
    curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl_arr[$i], CURLOPT_POSTFIELDS, $putData);
    curl_multi_add_handle($multi, $curl_arr[$i]);
}


$running = NULL;
do {
    curl_multi_exec($multi, $running);
} while ($running > 0);

foreach ($files as $i => $data) {
    curl_multi_remove_handle($multi, $curl_arr[$i]);
}
curl_multi_close($multi);


Please tell me what I'm doing wrong.
I found examples only for receiving multi-threaded and sending no.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
coderisimo, 2020-04-08
@coderisimo

Guzzle is very handy - docs.guzzlephp.org/en/stable/quickstart.html You can send requests asynchronously, you can wait until the entire request pool is completed and call the method when they complete, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question