• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

bigpipe+curlphp

php 搞代码 4年前 (2022-01-23) 35次浏览 已收录 0个评论
<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></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></code>
<code><span><?</span>phpinclude_once (<span>"Multicurl.class.php"</span>);<span>// include_once ("Singlecurl.class.php");</span><span>$body</span><span>=<<<</span>EOT<span><!</span>DOCTYPE html<span>></span><span><</span>html lang<span>=</span><span>"en"</span><span>></span><span><</span>head<span>></span><span><</span>meta charset<span>=</span><span>"UTF-8"</span><span>></span><span><</span>title<span>></span><span>111</span><span><</span>/title<span>></span><span><</span>/head<span>></span><span><</span>script src<span>=</span><span>"http://libs.baidu.com/jquery/1.9.0/jquery.js"</span><span>><</span>/script<span>></span><span><</span>body<span>></span><span><</span>div id<span>=</span><span>"div1"</span><span>><</span>/div<span>></span><span><</span>div id<span>=</span><span>"div2"</span><span>><</span>/div<span>></span><span><</span>div id<span>=</span><span>"div3"</span><span>><</span>/div<span>></span><span><</span>/body<span>></span><span><</span>script<span>></span>$(<span>"<link>"</span>)    <span>.</span>attr({ rel: <span>"stylesheet"</span>,        <span>type</span>: <span>"text/css"</span>,        href: <span>"./inc/body.css"</span>    })    <span>.</span>appendTo(<span>"head"</span>);$(<span>"<script>"</span>)    <span>.</span>attr({src: <span>"./inc/body.js"</span>})    <span>.</span>appendTo(<span>"head"</span>);<span><</span>/script<span>></span>EOT;echo <span>$body</span>;<span>$str_arr</span><span>=</span><span>array</span>(    <span>"div1"</span><span>=></span><span>"http://localhost/curltest/1.php"</span>,    <span>"div2"</span><span>=></span><span>"http://localhost/curltest/1.php"</span>,    <span>"div3"</span><span>=></span><span>"http://localhost/curltest/1.php"</span>,    );<span>$test</span><span>=</span><span>new</span> Multicurl(<span>$str_arr</span>);<span>// $test=new Sin<strong>(本文来源gaodai#ma#com搞@@代~&码网</strong><pre>搞代gaodaima码

glecurl($str_arr);$test->rolling_curl();?>

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了bigpipe+curlphp,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:bigpipe+curlphp
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址