Using cURL Handle as Array Key

I use curl_multi functions to request multiple URLs and process them as they complete. Since one connection completes all that I really have, it is a cURL descriptor (and related data) from curl_multi_info_read() .

The urls come from the job queue, and after processing I need to remove the job from the queue. I don't want to rely on the url to define the job (there shouldn't be duplicate urls, but what if there is one).

The solution I have developed so far is to use the cURL descriptor as the key of an array pointing to jobid. The form I can say when processed as a string, the handle looks something like this:

 "Resource id #1" 

These seams are reasonably unique to me. Main code:

 $ch = curl_init($job->getUrl()); $handles[$ch] = $job; //then later $done = curl_multi_info_read($master); $handles[$done['handle']]->delete(); curl_multi_remove_handle($master, $done['handle']); 

Is the cURL handle safe to use this way?

Or is there a better way to map cURL descriptors to the job he created?

+4
source share
3 answers

This will probably work thanks to some implicit type, but I don't like it at all. I think he asked for troubles somewhere in the queue, with future versions that handle resources differently, different platforms ...

I personally did not do this, but I used numerical indices.

+2
source

Store personal data inside a convenient cURL descriptor, for example. some work id:

 curl_setopt($ch, CURLOPT_PRIVATE, $job->getId()); // then later $id = curl_getinfo($done['handle'], CURLINFO_PRIVATE); 

This "private data" function has not yet been documented in the PHP manual. It was introduced already in PHP 5.2.4. It allows you to store and retrieve a string of your choice inside the cURL descriptor. Use it for a key that uniquely identifies a job.

Edit: The function is now documented in the PHP manual (find CURLOPT_PRIVATE on the page).

+6
source

I have to agree with Pekka ... it will probably work, but it smells bad. id uses direct integers, since Pekka offers or wraps handles in a simple class, and then uses spl_object_hash or has a constructor that generates uniqid when it is configured.

+2
source

Source: https://habr.com/ru/post/1301009/


All Articles