<code><span><span><?php</span><span><span>class</span><span>Multicurl</span>{</span><span>private</span><span>$param</span> = <span>array</span>(); <span>private</span><span>$config</span>= <span>null</span>; <span>public</span><span><span>function</span><span>__construct</span><span>(<span>$param</span>)</span>{</span><span>$this</span>->param = array_merge(<span>$this</span>->param,<span>$param</span>); } <span>public</span><span><span>function</span><span>rolling_curl</span><span>()</span>{</span><span>$chs</span> = curl_multi_init(); <span>foreach</span> (<span>$this</span>->param <span>as</span><span>$key</span> => <span>$value</span>) { <span>$ch</span> = curl_init(); <span>$curl</span>=<span>"{$value}?id={$key}"</span>; curl_setopt(<span>$ch</span>, CURLOPT_URL,<span>"{$curl}"</span>); curl_setopt(<span>$ch</span>, CURLOPT_HEADER, <span>0</span>); curl_setopt(<span>$ch</span>, CURLOPT_RETURNTRANSFER, <span>1</span>); curl_multi_add_handle(<span>$chs</span>,<span>$ch</span>); } <span>do</span>{ <span>if</span> ((<span>$status</span> = curl_multi_exec(<span>$chs</span>, <span>$active</span>)) != CURLM_CALL_MULTI_PERFORM) { <span>if</span> (<span>$status</span> != CURLM_OK) { <span>break</span>; } <span>//如果没有准备就绪,就再次调用curl_multi_exec</span><span>while</span> (<span>$done</span> = curl_multi_info_read(<span>$chs</span>)) { <span>$info</span> = curl_getinfo(<span>$done</span>[<span>"handle"</span>]); <span>$error</span> = curl_error(<span>$done</span>[<span>"handle"</span>]); <span>$result</span> = curl_multi_getcontent(<span>$done</span>[<span>"handle"</span>]); <span>echo</span><span>$result</span>; curl_multi_remove_handle(<span>$chs</span>, <span>$done</span>[<span>'handle'</span>]); curl_close(<span>$done</span>[<span>'handle'</span>]); <span>if</span> (<span>$active</span> > <span>0</span>) { curl_multi_select(<span>$chs</span>,<span>0.5</span>); <span>//此处会导致阻塞大概0.5秒。</span> } } } }<span>while</span>(<span>$active</span>); <span>//还有句柄处理还在进行中</span> } } <span>?></span></span></span></code>
<code><span><span><?php</span><span><span>class</span><span>Singlecurl</span>{</span><span>private</span><span>$param</span> = <span>array</span>(); <span>private</span><span>$config</span>= <span>null</span>; <span>public</span><span><span>function</span><span>__construct</span><span>(<span>$param</span>)</span>{</span><span>$this</span>->param = array_merge(<span>$this</span>->param,<span>$param</span>); } <span>public</span><span><span>function</span><span>rolling_curl</span><span>()</span>{</span><span>foreach</span> (<span>$this</span>->param <span>as</span><span>$key</span> => <span>$value</span>) { <span>$ch</span> = curl_init(); <span>$curl</span>=<span>"{$value}?id={$key}"</span>; curl_setopt(<span>$ch</span>, CURLOPT_URL,<span>"{$curl}"</span>); curl_setopt(<span>$ch</span>, CURLOPT_HEADER, <span>0</span>); curl_setopt(<span>$ch</span>, CURLOPT_RETURNTRANSFER, <span>1</span>); <span>$output</span> = curl_exec ( <span>$ch</span> ); <span>echo</span><span>$output</span>; <span>$tim</span>=<span>"tim="</span>.microtime(<span>true</span>).<span>"\r\n"</span>; file_put_contents ( <span>"test.txt"</span> , <span>$tim</span> , FILE_APPEND|LOCK_EX ); } } } <span>?></span></span></span></code>
著作権表示: この記事はブロガーによるオリジナル記事であり、ブロガーの許可なく複製することはできません。
上記では、関連する内容も含めて bigpipe+curlphp を紹介しましたが、PHP チュートリアルに興味のある友人に役立つことを願っています。