Maison >développement back-end >tutoriel php > php-httpsqs-client 的施用
php-httpsqs-client 的使用
安装说明
<span class="pln">mkdir php_httpsqs_client? cd php_httpsqs_client? <br >unzip php_httpsqs_0</span><span class="pun">.</span><span class="lit">1.zip</span><span class="pln">? </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">php</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">phpize? </span><span class="pun">./</span><span class="pln">configure </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">httpsqs <br ></span><span class="pun">--</span><span class="kwd">with</span><span class="pun">-</span><span class="pln">php</span><span class="pun">-</span><span class="pln">config</span><span class="pun">=</span><span class="str">/usr/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">php</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">php</span><span class="pun">-</span><span class="pln">config? make </span><span class="pun">&&</span><span class="pln"> make install? </span><span class="com"><br >#接着在php.ini中添加一行</span><span class="pln">? extension</span><span class="pun">=</span><span class="pln">httpsqs</span><span class="pun">.</span><span class="pln">so</span><span class="pun">;</span>
函数说明
<span class="com">/**? ?* 创建httpsqs连接? ?* @param string $host 服务器地址,可以为空,默认为127.0.0.1? ?* @param int ? ?$port 服务器端口,可以为空,默认为1218? ?* @return resource? ?*/</span><span class="pln">? $hr </span><span class="pun">=</span><span class="pln"> httpsqs_connect</span><span class="pun">(</span><span class="str">"127.0.0.1"</span><span class="pun">,</span><span class="pln"> </span><span class="lit">1218</span><span class="pun">);</span>
<span class="com">/**? ?* 写入队列数据? ?* @param resource $hr ? ? ?服务器连接句柄? ?* @param string ? $queue ? 队列名称? ?* @param string ? $data ? ?写入数据? ?* @param string ? $charset 字符集,可以为空,默认为utf-8? ?* @return boolean? ?*/</span><span class="pln">? <br >$putRes </span><span class="pun">=</span><span class="pln"> httpsqs_put</span><span class="pun">(</span><span class="pln">$hr</span><span class="pun">,</span><span class="pln"> </span><span class="str">"testQueue"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"This is a test Data"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"UTF-8"</span><span class="pun">);</span>
<span class="com">/** ? ?* 获取队列最后一条数据? ?* @param resource $hr? ?* @param string ? $queue? ?* @param boolean ?$return_array 是否返回数组,可以为空,默认为false? ? ? ? ? ? 返回数组格式:array('pos'=>'队列插入点', 'data'=>'数据值')? ?* @param string ? $charset 可以为空? ?* @return mixed? ?*/</span><span class="pln">? $content </span><span class="pun">=</span><span class="pln"> httpsqs_get</span><span class="pun">(</span><span class="pln">$hr</span><span class="pun">,</span><span class="pln"> </span><span class="str">"testQueue"</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">,</span><span class="pln"> </span><span class="str">"UTF-8"</span><span class="pun">);</span>
<span class="com">/**? ?* 获取队列状态? ?* @param resource $hr? ?* @param string ? $queue? ?* @param boolean ?$return_json 是否返回状态的json格式,可以为空,默认为false? ?* @return string? ?*/</span><span class="pln">? $status </span><span class="pun">=</span><span class="pln"> httpsqs_status</span><span class="pun">(</span><span class="pln">$hr</span><span class="pun">,</span><span class="pln"> </span><span class="str">"testQueue"</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">);</span>
<span class="com">/**? ?* 获取队列某个点数据? ?* @param resource $hr? ?* @param string ? $queue? ?* @param int ? ? ?$pos 要获取的某条数据的位置? ?* @param string ? $charset 可以为空? ?* @return string? ?*/</span><span class="pln">? $posData </span><span class="pun">=</span><span class="pln"> httpsqs_view</span><span class="pun">(</span><span class="pln">$hr</span><span class="pun">,</span><span class="pln"> </span><span class="str">"testQueue"</span><span class="pun">,</span><span class="pln"> </span><span class="lit">10</span><span class="pun">,</span><span class="pln"> </span><span class="str">"UTF-8"</span><span class="pun">);</span>
<span class="com">/**? ?* 队列重置? ?* @param resource $hr? ?* @param string ? $queue? ?* @return boolean? ?*/</span><span class="pln">? $resetRes </span><span class="pun">=</span><span class="pln"> httpsqs_reset</span><span class="pun">(</span><span class="pln">$hr</span><span class="pun">,</span><span class="pln"> </span><span class="str">"testQueue"</span><span class="pun">);</span>
<span class="com">/**? ?* 设置队列最大数据条数? ?* @param resource $hr? ?* @param string ? $queue? ?* @param int ? ? ?$maxqueue 队列最大数据条数? ?* @return boolean? ?*/</span><span class="pln">? $maxqueueRes </span><span class="pun">=</span><span class="pln"> httpsqs_maxqueue</span><span class="pun">(</span><span class="pln">$hr</span><span class="pun">,</span><span class="pln"> </span><span class="str">"testQueue"</span><span class="pun">,</span><span class="pln"> </span><span class="lit">10000</span><span class="pun">);</span>
<span class="com">/**? ?* 修改定时刷新内存缓冲区内容到磁盘的间隔时间? ?* @param resource $hr? ?* @param string ? $queue? ?* @param int ? ? ?$synctime 间隔时间? ?* @return boolean? ?*/</span><span class="pln">? $synctimeRes </span><span class="pun">=</span><span class="pln"> httpsqs_synctime</span><span class="pun">(</span><span class="pln">$hr</span><span class="pun">,</span><span class="pln"> </span><span class="str">"testQueue"</span><span class="pun">,</span><span class="pln"> </span><span class="lit">10</span><span class="pun">);</span>
对象调用
<span class="com">// 参数与httpsqs_connect对应</span><span class="pln">? $hr </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">HttpSQS</span><span class="pun">(</span><span class="pln">$host</span><span class="pun">,</span><span class="pln"> $port</span><span class="pun">);</span><span class="pln">? ? </span><span class="com">// 参数与httpsqs_get对应</span><span class="pln">? $hr</span><span class="pun">-></span><span class="kwd">get</span><span class="pun">(</span><span class="pln">$queuename</span><span class="pun">,</span><span class="pln"> $return_array</span><span class="pun">,</span><span class="pln"> $charset</span><span class="pun">);</span><span class="pln">? ? </span><span class="com">// 参数与httpsqs_put对应</span><span class="pln">? $hr</span><span class="pun">-></span><span class="pln">put</span><span class="pun">(</span><span class="pln">$queuename</span><span class="pun">,</span><span class="pln"> $data</span><span class="pun">,</span><span class="pln"> $charset</span><span class="pun">);</span><span class="pln">? ? </span><span class="com">// 参数与httpsqs_status对应</span><span class="pln">? $hr</span><span class="pun">-></span><span class="pln">status</span><span class="pun">(</span><span class="pln">$queuename</span><span class="pun">,</span><span class="pln"> $return_json</span><span class="pun">);</span><span class="pln">? ? </span><span class="com">// 参数与httpsqs_view对应</span><span class="pln">? $hr</span><span class="pun">-></span><span class="pln">view</span><span class="pun">(</span><span class="pln">$queuename</span><span class="pun">,</span><span class="pln"> $pos</span><span class="pun">);</span><span class="pln">? ? </span><span class="com">// 参数与httpsqs_reset对应</span><span class="pln">? $hr</span><span class="pun">-></span><span class="pln">reset</span><span class="pun">(</span><span class="pln">$queuename</span><span class="pun">);</span><span class="pln">? ? </span><span class="com">// 参数与httpsqs_maxqueue对应</span><span class="pln">? $hr</span><span class="pun">-></span><span class="pln">maxqueue</span><span class="pun">(</span><span class="pln">$queuename</span><span class="pun">);</span><span class="pln">? ? </span><span class="com">// 参数与httpsqs_synctime对应</span><span class="pln">? $hr</span><span class="pun">-></span><span class="pln">synctime</span><span class="pun">(</span><span class="pln">$queuename</span><span class="pun">);</span>
示例
<span class="com">// 取数据Daemon</span><span class="pln">? $hr </span><span class="pun">=</span><span class="pln"> httpsqs_connect</span><span class="pun">(</span><span class="pln">$host</span><span class="pun">,</span><span class="pln"> $port</span><span class="pun">);</span><span class="pln">? </span><span class="kwd">while</span><span class="pln"> </span><span class="pun">(</span><span class="lit">1</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">? ? ? <br >$data </span><span class="pun">=</span><span class="pln"> httpsqs_get</span><span class="pun">(</span><span class="pln">$hr</span><span class="pun">,</span><span class="pln"> $queuename</span><span class="pun">,</span><span class="pln"> $charset</span><span class="pun">);</span><span class="pln">? ? ? </span><span class="kwd"><br >if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">$data </span><span class="pun">===</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">? ? ? ? ?sleep</span><span class="pun">(</span><span class="lit">1</span><span class="pun">);</span><span class="pln">? ? ? </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span><span class="pln">? ? ? ? </span><span class="com">// do something...</span><span class="pln">? ? ? </span><span class="pun">}</span><span class="pln">? </span><span class="pun">}</span><span class="pln">? ?<br >?</span><span class="com">// 或者</span><span class="pln">? $hr </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">HttpSQS</span><span class="pun">(</span><span class="pln">$host</span><span class="pun">,</span><span class="pln"> $port</span><span class="pun">);</span><span class="pln">? </span><span class="kwd">while</span><span class="pln"> </span><span class="pun">(</span><span class="lit">1</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">? ? ? $data </span><span class="pun">=</span><span class="pln"> $hr</span><span class="pun">-></span><span class="kwd">get</span><span class="pun">(</span><span class="pln">$queuename</span><span class="pun">,</span><span class="pln"> $charset</span><span class="pun">);</span><span class="pln">? ? ?<br >?</span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">$data </span><span class="pun">===</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">? ? ? ? ? sleep</span><span class="pun">(</span><span class="lit">1</span><span class="pun">);</span><span class="pln">? ? ? </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span><span class="pln">? ? ? ? </span><span class="com">// do something...</span><span class="pln">? ? ? </span><span class="pun">}</span><span class="pln">? </span><span class="pun">}</span><span class="pln">? ?<br >?</span><span class="com">// 写数据</span><span class="pln">? $hr </span><span class="pun">=</span><span class="pln"> httpsqs_connect</span><span class="pun">(</span><span class="pln">$hort</span><span class="pun">,</span><span class="pln"> $port</span><span class="pun">);</span><span class="pln">? httpsqs_put</span><span class="pun">(</span><span class="pln">$hr</span><span class="pun">,</span><span class="pln"> $queuename</span><span class="pun">,</span><span class="pln"> $data</span><span class="pun">,</span><span class="pln"> $charset</span><span class="pun">);</span><span class="pln">? ?<br >?</span><span class="com">// 或者</span><span class="pln">? $hr </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">HttpSQS</span><span class="pun">(</span><span class="pln">$hort</span><span class="pun">,</span><span class="pln"> $port</span><span class="pun">);</span><span class="pln">? $hr</span><span class="pun">-></span><span class="pln">put</span><span class="pun">(</span><span class="pln">$queuename</span><span class="pun">,</span><span class="pln"> $data</span><span class="pun">,</span><span class="pln"> $charset</span><span class="pun">);</span>