我无法在多卷曲场景中找到有关特定卷曲手柄的更多信息.这是代码.
$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 ) ;
Run Code Online (Sandbox Code Playgroud)
所以在这里我循环使用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 ) ) ;
Run Code Online (Sandbox Code Playgroud)
但是在完成每个curl请求之后,我找不到发送正确的产品名称和id的方法,该名称和id对应于已完成的请求.当在上面的代码中我将名称,id和输出传递给PrStacker线程时,我意识到它不是与完成的请求相对应的正确产品.它是一个传递给线程的不同且错误的产品.
那么有什么方法可以在执行之前将产品名称和id以及每个curl句柄/请求包括在内,以便程序可以识别哪个响应对应于哪个产品.我希望我的解释能够被理解.
如果有任何方法可以做到,请告诉我.
将私有数据存储在cURL easy handle中,例如产品ID:
curl_setopt($curl_handle, CURLOPT_PRIVATE, $this->getId());
// then later
$id = curl_getinfo($done['handle'], CURLINFO_PRIVATE);
Run Code Online (Sandbox Code Playgroud)
这个"私有数据"功能直到2015年初才在PHP手册中记录.它已在PHP 5.2.4中引入.它允许您在cURL句柄中存储和检索您选择的字符串.将其用于唯一标识产品的密钥/ ID,以及可用于在您自己的数据结构中查找产品的密钥/ ID.
请参阅:curl_getinfo和curl的预定义常量.
要求是将两个 php“对象”相互关联。一个由“cUrl 句柄”标识,另一个是“产品详细信息”,它是“php 对象”或“实例”。在这种情况下,“cUrl 句柄”将用作键。
将“对象”关联在一起的方法有多种。问题是没有可以使用的“明显的密钥”。
尽管如此,PHP“数组”(哈希映射)确实是非常灵活的数据结构。它们可以存储任何标准 PHP 对象。尴尬的部分可能是获得一个有用的“钥匙”来再次查找它们。
以下是一些行之有效的有用方法:
1) SplObjectStorage 类,允许您使用对象本身作为键将数据与对象关联起来。它是通过示例进行描述的:class.splobjectstorage
2) 使用“cUrl Handle”的“Spl_Object_Hash”作为键,将“ProductDetails”对象存储在数组中。