我们在PHP的cURL中使用CURLOPT_WRITEFUNCTION是什么? [英] What for do we use CURLOPT_WRITEFUNCTION in PHP's cURL?

查看:429
本文介绍了我们在PHP的cURL中使用CURLOPT_WRITEFUNCTION是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以用例子来描述吗?

Could you describe it in examples, please?

推荐答案

我知道这是一个老问题,对你或别人有一些帮助。 WRITEFUNCTION对于处理文本时非常有用,因为它在流式传输中或根据某些条件中止下载。下面的例子简单地将所有的文本放入大写字母:

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;
}

要查看我如何使用它,请查看此链接:具有WRITEFUNCTION回调的并行cURL请求

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

这篇关于我们在PHP的cURL中使用CURLOPT_WRITEFUNCTION是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆