如何将额外的数据存储到多卷曲信息处理程序中?

2024-03-03

我无法在多卷曲场景中找到有关特定卷曲句柄的更多信息。这是代码。

$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 个产品,我初始化卷曲手柄,并将其添加到卷曲多手柄中。

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

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

但是在每个卷曲请求完成后,我找不到发送与已完成的请求相对应的正确产品名称和 ID 的方法。当在上面的代码中我将名称、id 和输出传递到 PrStacker 线程时,我意识到它不是与已完成的请求相对应的正确产品。它是传递给线程的不同且错误的产品。

那么,有什么方法可以在执行之前将产品名称和 id 与每个curl 句柄/请求一起包含在内,以便程序可以识别哪个响应对应于哪个产品。我希望我的解释能够被理解。

请告诉我是否有任何方法可以做到这一点。


将私有数据存储在 cURL Easy Handle 中,例如产品编号:

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

这种“私有数据”功能直到 2015 年初才记录在 PHP 手册中。它已在 PHP 5.2.4 中引入。它允许您在 cURL 句柄内存储和检索您选择的字符串。将其用作唯一标识产品的密钥/ID,并且您可以使用它在自己的数据结构中查找产品。

See: curl_getinfo https://secure.php.net/manual/ro/function.curl-getinfo.php and curl 的预定义常量 https://secure.php.net/manual/en/curl.constants.php.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将额外的数据存储到多卷曲信息处理程序中? 的相关文章

随机推荐