php - CURL Multi获取关于特定句柄的信息 [英] php - CURL Multi get info about a particular handle

查看:103
本文介绍了php - CURL Multi获取关于特定句柄的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在多卷曲情况下,我无法找到有关特定卷曲句柄的更多信息。这是代码。

I'm having trouble finding more info about a particular curl handle in a multi curl scenario. Here is the code .

$job_count = 5;
 while ( $eachPr = $prList->fetch () ) {


            for ( $job_number = 1 ;
                        $job_number <= $job_count ;
                        $job_number ++ , $index ++ ) {


                $url = $this->getURL ( $eachPr[ "name" ] ,
                                       $eachPr[ "category" ] ) ;

                $this->log ( $url ) ;



                $curl_handle = curl_init () ;

                curl_setopt ( $curl_handle ,
                              CURLOPT_USERAGENT ,
                              $userAgent ) ;
                curl_setopt ( $curl_handle ,
                              CURLOPT_URL ,
                              $url ) ;
                curl_setopt ( $curl_handle ,
                              CURLOPT_FAILONERROR ,
                              TRUE ) ;
                curl_setopt ( $curl_handle ,
                              CURLOPT_FOLLOWLOCATION ,
                              TRUE ) ;
                curl_setopt ( $curl_handle ,
                              CURLOPT_AUTOREFERER ,
                              TRUE ) ;
                curl_setopt ( $curl_handle ,
                              CURLOPT_RETURNTRANSFER ,
                              TRUE ) ;

                curl_setopt ( $curl_handle ,
                              CURLOPT_COOKIE ,
                              $cookie ) ;
                var_dump($curl_handle);

                /* add a request to the multiple handle */
                curl_multi_add_handle ( $multi_handler ,
                                        $curl_handle ) ;
                $eachPr = $prList->fetch () ;
            }


            do {
                while ( ($execrun = curl_multi_exec ( $multi_handler ,
                                                      $running )) == CURLM_CALL_MULTI_PERFORM )  ;
                if ( $execrun != CURLM_OK ) {
                    break ;
                }
                /* a request was just completed -- find out which one */
                while ( $done = curl_multi_info_read ( $multi_handler ) ) {

                    /* get the info and content returned on the request */
                    $info   = curl_getinfo ( $done[ 'handle' ] ) ;
                    $output = curl_multi_getcontent ( $done[ 'handle' ] ) ;
                    var_dump($info);
                    /* send the return values to the thread waiting to process the data . 
                    $this->work_pool[] = $this->submit ( new PrStacker ( $eachPr[ "name" ] ,
                                                                                        $eachPr[ "id" ] ,
                                                                                        $output ) ) ;

                    $this->work_pool[ count ( $this->work_pool ) - 1 ]->wait () ;


                    /* remove the curl handle that just completed */
                    curl_multi_remove_handle ( $multi_handler ,
                                               $done[ 'handle' ] ) ;


                }

                /* Block for data in / output; error handling is done by curl_multi_exec */
                if ( $running ) {
                    curl_multi_select ( $multi_handler ,
                                        30 ) ;
                }
            } while ( $running ) ;


            /* write the current index to the file */
            file_put_contents ( $symbols_index_file ,
                                $index ) ;

            $sleep_interval = rand ( 5 ,
                                     10 ) ;

            $this->log ( " Sleeping Now For " . $sleep_interval . "  seconds" ) ;

            sleep ( $sleep_interval ) ;

            $index ++ ;
        }
        curl_multi_close ( $multi_handler ) ;

因此,在这里使用循环通过一个11K产品列表while eachPr = $ prList-> fetch())。然后一次5个产品我初始化卷曲句柄,我添加到卷曲多句柄。

So in here im looping through a list of 11K product using while ( $eachPr = $prList->fetch () ). Then taking 5 product at a time i'm initializing curl handles which i add to the curl multi handle.

句柄在do while循环执行。这里遇到麻烦,选择刚刚使用 $ done = curl_multi_info_read($ multi_handler)完成的请求。
每个响应传递给另一个处理其他任务的线程。每个线程都需要产品名称,产品ID和原始html响应。
这里是每个堆栈器被初始化的方式

The handles are executed at the do while loop. Here comes the trouble after selecting the request which was just done using $done = curl_multi_info_read ( $multi_handler ) . Each response is passed to another thread which handles other task. And each thread requires the product name, the product id and the raw html response. Here this is how each of the stackers are initialized

$this->work_pool[] = $this->submit ( new PrStacker ( $eachPr[ "name" ] ,
                                                                                            $eachPr[ "id" ] ,
                                                                                            $output ) ) ;

但是在每个curl请求完成后,我找不到一个方法来发送正确的产品名称和ID,对应于已完成的请求。当在上面的代码中,我通过名称,id和输出到PrStacker线程,我意识到,这不是正确的产品对应的请求,是做了。它是一个不同的错误的产品,传递给线程。

But after each of the curl requests gets done i can't find a way to send the correct product name and id which corresponds to the request that was finished. When in the above code i pass the name, id and output to the PrStacker thread i realized that it was not the correct product that corresponds to the request which was done. Its a different and wrong product that gets passed to the threads.

所以有任何方式,我可以包括产品名称和id与每个curl句柄/请求,以便程序可以识别哪个响应对应于哪个产品。我希望我的解释可以理解。

So is there any way in which i can include the product name and id along with each of the curl handles/requests before it gets executed so that the program may recognize which response corresponds to which product. I hope my explanation can be understood.

请让我知道如果有任何方法可以做到,我真的卡在这里。

Please let me know if there is any way in which this can be done, i'm really stuck here.

推荐答案

要求是将两个php对象相互关联。一个由cUrl句柄标识,另一个是产品详细信息,它是一个php对象或实例。在这种情况下,'cUrl句柄'将用作键。

The requirement is to associate two php 'objects' with each other. One is identified by a 'cUrl handle', the other is 'Product Details' which is a php object' or 'instance. The 'cUrl handle' will be used as the key in this case.

有多种方法可以将对象关联在一起。问题是没有
'明显的键'可以使用。

There are various ways of associating 'objects' together. The issue is that there are no 'obvious keys' that can be used.

然而,PHP'arrays'(哈希映射)确实是非常灵活的数据结构。它们可以存储任何标准的PHP对象。尴尬的部分可以得到一个有用的关键,再次查找他们。

Still, PHP 'arrays' (hash maps) are very flexible data structures indeed. They can store any standard PHP object. The awkward part can be getting a useful 'key' to look them up again.

以下是一些有用的方法:

Here are some useful approaches that will work:

1)SplObjectStorage类允许你关联数据与使用对象本身作为关键的对象。以示例说明: class.splobjectstorage

1) The SplObjectStorage class which allows you to associate data with objects using the object itself as a key. It is described, with examples: class.splobjectstorage

2)使用'cUrl Handle'的'Spl_Object_Hash'作为键,将'ProductDetails'对象存储在数组中。

2) store the 'ProductDetails' object in an array, using the 'Spl_Object_Hash' of the 'cUrl Handle', as the key.

这篇关于php - CURL Multi获取关于特定句柄的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆