curl在php中的使用

来源:互联网 发布:mac设置桌面应用 编辑:程序博客网 时间:2024/05/16 12:05

随着版本的不同curl在php中试用也变得有所不同,对于curl的资料,大家也是迷惑,确实关于curl,在网上还是能找到一些用法的,但是没有什么解释,今天,我就把自己所找到的资料 跟大家分享一下

首先是一些函数


curlm_multi_init

This function returns a CURLM handle to be used as input to all the other multi-functions, sometimes referred to as a multi handle in some places in the documentation. This init call MUST have a corresponding call to curl_multi_cleanup when the operation is complete.

这一个函数返回一个CURLM句柄,该句柄将会被用作其他所有的multi-函数的输入使用,也就是作为参数传递,这个初始化的call必须有一个相对应的函数调用curl_multi_cleanup,在操作完成的时候.

curl_multi_exec

该函数实际上调用了curl的底层的curl_multi_perform函数,我们来看一下,文档对他的描述

最为简单的描述是这样的;

reads/writes available data from each easy handle

翻译成中文就是;从每一个句柄中读或者写可获得数据

下面有一段话

This function handles transfers on all the added handles that need attention in an non-blocking fashion.

When an application has found out there's data available for the multi_handle or a timeout has elapsed, the application should call this function to read/write whatever there is to read or write right now etc.curl_multi_perform returns as soon as the reads/writes are done. This function does not require that there actually is any data available for reading or that data can be written, it can be called just in case. It will write the number of handles that still transfer data in the second argument's integer-pointer.

If the amount of running_handles is changed from the previous call (or is less than the amount of easy handles you've added to the multi handle), you know that there is one or more transfers less "running". You can then call curl_multi_info_read to get information about each individual completed transfer, and that returned info includes CURLcode and more. If an added handle fails very quickly, it may never be counted as a running_handle.

When running_handles is set to zero (0) on the return of this function, there is no longer any transfers in progress.

我们来看这段话到底说了什么:

这个函数处理所有的被添加的句柄上的数据传输,在一个引用程序发现有数据需要处理的时候或者超时发生了,应用程序应该调用这个函数,该函数在读或者写,完毕的时候返回,这个函数不要求,真的存在着数据需要读取或者写入,他可以在任何情况下被调用,调用玩这个函数之后,他将会设置传递给它的第二个参数(running -handles),该参数指示还有多少个活动连接

如果running-handles从前一次的调用中被调用了,那说明该传输已经完成了,或者出现了传输错误,为了查看每一个连接的传输情况,你需要调用curl-multi-read-info函数,他会返回一个包含一个三个数据的数组,具体请看php帮助文档。

当running-handles被curl设置为0的时候,那么这意味着已经完成所有的传输了。

你可能很迷惑它的返回值到底是什么,是不是??

CURLMcode type, general libcurl multi interface error code.

Before version 7.20.0: If you receive CURLM_CALL_MULTI_PERFORM, this basically means that you should call curl_multi_perform again, before you select() on more actions. You don't have to do it immediately, but the return code means that libcurl may have more data available to return or that there may be more data to send off before it is "satisfied". Do note that curl_multi_perform will returnCURLM_CALL_MULTI_PERFORM only when it wants to be called again immediately. When things are fine and there is nothing immediate it wants done, it'll return CURLM_OK and you need to wait for "action" and then call this function again.

This function only returns errors etc regarding the whole multi stack. Problems still might have occurred on individual transfers even when this function returns CURLM_OK. Use curl_multi_info_read to figure out how individual transfers did.

在7.20.0版本之前,如果你收到CURLM_CALL_MULTI_PERFORM,那么意味着,你应该再次调用curl_multi_perform函数,在你调用curl_multi_select之前,在没有他要处理的数据的时候,他会返回CURLM_OK,你只需要等待动作(暂时理解action为动作吧,因为我实在找不到比较好的词了),如果CURLM_OK被返回,那么你只需要等待,直到(注意了),这里是我们调用的curl_multi_select,他只返回值为-1,不管怎么样,他不会等待什么动作,只是在usleep这段时间之后,curl-multi-perform还是会被调用,可能会没有什么意义,但没办法

curl_multi_select

先说一下这个函数的作用吧

对于以下代码
while($still_running && $result==CURLM_OK)
{    
do
{
     $result=curl_multi_exec($mh,$still_running);
}while($result==CURLM_CALL_MULTI_PERFORM);
}

这段代码中如果你不用curl_multi_select的话,你会发现你的cpu搞得离谱,你可以测试一下,这会影响你的cpu使用效率,因为他会持续调用这段没有什么意义的代码(在某一段时间内是的)

这个函数在php帮助文档中解释为
阻塞直到cURL批处理连接中有活动连接
也就是说如果没有检测到有数据传输,他会阻塞,但是你要注意了,在当前的版本中以下代码是不适用的,

        
while($still_running && $result==CURLM_OK)
{    
  if(curl_multi_select($mh)!=-1)
do
{
     $result=curl_multi_exec($mh,$still_running);
}while($result==CURLM_CALL_MULTI_PERFORM);
}
}

真的很不幸,这段代码,会陷入死循环,追其原因,你会发现curl_multi_select只返回-1,也就是说他里面的内容从没有被调用,该怎么做呢,至少在你看到这段代码的时候是没有错误的

while($still_running && $result==CURLM_OK)
{    
  if(curl_multi_select($mh)==-1)
    usleep(100);//这是需要你自己写的,具体的你自己定,官方推荐100ms
}
do
{
     $result=curl_multi_exec($mh,$still_running);
}while($result==CURLM_CALL_MULTI_PERFORM);

}
这样你就发现,服务器返回数据了,蛮快的

curl_multi_info_read

这个函数在php帮助文档中说的很清楚了
查询批处理句柄是否单独的传输线程中有消息或信息返回。消息可能包含诸如从单独的传输线程返回的错误码或者只是传输线程有没有完成之类的报告。
成功时返回相关信息的数组,失败时返回FALSE
你只需要关心返回值就可以了

据我测试要存在消息,只有可能有两种情况:
1.传输已经完成了
2.传输出现错误

array(3) { ["msg"]=> int(1) ["result"]=> int(0) ["handle"]=> resource(5) of type (curl) }
这是我的结果

curl_multi_getcontent($res)

该函数就简单多了
如果设置了CURLOPT_RETURNTRANSFER,则返回获取的输出的文本流
注意$res参数是某一个curl句柄


下面给大家推荐一个网址:
http://curl.haxx.se/这里详细讨论了curl的方方面面,当然作为使用者,我们只需要知道怎么来使用它就可以了,
这里面给我们介绍了,如何来设置curl_setopt的参数,毕竟curl的一切玄机仅在于此




0 0
原创粉丝点击