curl在php中的使用

WBOY
發布: 2016-07-29 09:15:06
原創
952 人瀏覽過

隨著版本的不同curl在php中試用也變得有所不同,對於curl的資料,大家也是迷惑,確實關於curl,在網上還是能找到一些用法的,但是沒有什麼解釋,今天,我就把自己所找到的資料跟大家分享一下

首先是一些函數

curlm_multi_init

This function returns a.UR LM. 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_multi_exec

最簡單的描述是這樣的;

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

When an application has found out there's s unhable the madul the madul the mulh theunc the func the mot. tion 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. 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 multihandle trans the multio the handles you've added to the multi handle ), yous less "running". You can then call curl_multi_info_read to get information about each individual completed transfer, and that returned info include

s CURLcode and more. If。 , 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 c  return

CURLM_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 andand 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 occur刪除 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_)

while($still_running && $result==CURLM_)

while($still_running && $result==CURLM_)

{

     $result= curl_multi_exec($mh,$still_running);

}while($result==CURLM_CALL_MULTI_PERFORM);

}_7 如果你可以搞得離去的測試一下,這會影響你的cpu使用效率,因為他會持續調用這段沒有什麼意義的程式碼(在某一段時間內是的)

這個函數在php幫助文檔中解釋為

阻塞直到cURL批處理連線中有活動連線

也就是說如果沒有偵測到有資料傳輸,他會阻塞,但是你要注意了,在目前的版本中以下程式碼是不適用的,

        

while($still_running && $result==CURLM_OK){    

  if(curl_multi_select($mh)!=-1){ 🎠 =curl_multi_exec($mh,$still_running) ;

}while($result==CURLM_CALL_MULTI_PERFORM);

}

}

真的很不幸,這段程式碼,會陷入死

}

真的很不幸,這段程式碼,會陷入死

循環循環循環1,也就是說他裡面的內容從沒有被調用,該怎麼做呢,至少在你看到這段代碼的時候是沒有錯誤的

while($still_running && $result==CURLM_OK)

{

  if(curl_multi_select($mh)==-1)

    usleep(100);//這是需要你自己寫的,00ms  do

{

   do

{

     $result=curl_multi_exec($mh,$still_running);

}while($result==CURLM_CALLning);

}while($result==CURLM_CALL_RMA_PERPERCALL ,伺服器回傳資料了,蠻快的

curl_multi_info_read

這個函數在php幫助文檔中說的很清楚了

查詢批次句柄是否單獨的傳輸線程中有訊息或資訊返回。訊息可能包含諸如從單獨的傳輸線程返回的錯誤碼或只是傳輸線程有沒有完成之類的報告。

成功時傳回相關資訊的數組,失敗時回傳FALSE

你只需要關心返回值就可以了

據我測試要存在消息,只有可能有兩種情況:

1.傳輸已經完成了

2.傳輸出現錯誤

3) { ["msg"]=> int(1) ["result"]=> int(0) ["handle"]=> resource(5) of type (curl) }

這是我的結果


curl_multi_getcontent($res)

該函數就簡單多了

4個設定了的文字

注意$res參數是某一個curl句柄

下面給大家推薦一個網址:

http://curl.haxx.se/這裡詳細討論了curl的方方面面,當然作為使用者,我們只需要知道怎麼來使用它就可以了,

在這裡面給我們介紹了,如何來設定curl_setopt的參數,畢竟curl的一切玄機僅在於此

以上就介紹了curl在php中的使用,包括了方面的內容,希望對PHP教程有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!