swoole / library

📚 Swoole Library
https://wiki.swoole.com/#/library
Apache License 2.0
239 stars 56 forks source link

Fix CURLOPT_WRITEFUNCTION #74

Closed sy-records closed 3 years ago

sy-records commented 3 years ago
// composer require guzzlehttp/guzzle

include __DIR__ . '/vendor/autoload.php';

use GuzzleHttp\Client;

Swoole\Runtime::enableCoroutine(SWOOLE_HOOK_ALL);

go(function () {
    $url = 'https://httpbin.org/get';
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
//    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $data) {
        echo 'Data: ' . $data;
        return strlen($data);
    });

    $data = curl_exec($ch);
    curl_close($ch);
    var_dump($data);
//
//    $client = new Client();
//    $url = 'http://baidu.com';
//    $res = $client->request('GET', $url);
//    var_dump($res->getBody()->getContents());
});