开发者

What for do we use CURLOPT_WRITEFUNCTION in PHP's cURL?

Could you describe it 开发者_Python百科in examples, please?


I know this is an old question, but maybe my answer will be of some help for you or someone else. The WRITEFUNCTION is useful for processing text as it comes streaming in or for aborting the download based on some condition. Here's an example that simply puts all the text into uppercase letters:

function get_html($url){
    $ch = curl_init();
    $obj = $this;//create an object variable to access class functions and variables
    $this->result = '';
    $callback = function ($ch, $str) use ($obj) {
        $obj->result .= strtoupper($str);
        return strlen($str);//return the exact length
    };
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_WRITEFUNCTION, $callback);
    curl_exec($ch);
    curl_close($ch);
    return $this->result;
}

To see how I used it, check out this link: Parallel cURL Request with WRITEFUNCTION Callback.


It is used with curl_setopt function.

CURLOPT_WRITEFUNCTION is the name of a callback function where the callback function takes two parameters. The first is the cURL resource, and the second is a string with the data to be written. The data must be written by using this callback function. Must return the exact number of bytes written or this will fail.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜