phpcurl / curlwrapper Goto Github PK
View Code? Open in Web Editor NEWThe simplest OOP cURL wrapper for PHP
Home Page: https://packagist.org/packages/phpcurl/curlwrapper
License: MIT License
The simplest OOP cURL wrapper for PHP
Home Page: https://packagist.org/packages/phpcurl/curlwrapper
License: MIT License
I understand that this library maps the native curl_ functions almost identically, but I am observing different behaviour between CurlMulti::getContent
and curl_multi_get_content
.
I have set up a page at http://52.48.57.141/slow.php?num=10000 that slowly returns a specified number of numbers, all on new lines. The content is flushed as regularly as possible, and the webserver handles this by sending the Content-encoding: chunked
header.
View the page in a browser, and you'll see the chunked content being streamed to the page.
When a curl handle is added to curl multi, it is possible to call curl_multi_get_content
to retrieve the response in chunks as it is flushed. It works as expected when calling the native function:
$content = curl_multi_get_content($ch);
// $content is the latest chunk of the response.
However, when changing the native functions to use this library, the whole response is returned by calling getContent:
$content = $curlMulti->getContent($ch);
// $content is the whole request, up to the latest chunk, including previously returned chunks.
I have written a short program to show this in action using native functions, and rewritten it using this library. Please let me know if there is something I'm doing wrong.
Note: even though I'm using curl_multi, I'm actually only using one curl request, to keep it simple to see the problem.
<?php
$ch = curl_init("http://52.48.57.141/slow.php?num=10000");
$curlMulti = curl_multi_init();
curl_multi_add_handle($curlMulti, $ch);
$done = false;
do {
do {
$status = curl_multi_exec(
$curlMulti,
$active
);
}
while($status === CURLM_CALL_MULTI_PERFORM);
if($active === 0) {
$done = true;
}
$content = curl_multi_getcontent($ch);
echo $content;
}
while($done === false);
echo "DONE!";
The above script outputs the following in chunks (truncated using ellipsis):
0
1
2
3
4
5
6
7
8
9
10
11
...
9996
9997
9998
9999
DONE!
<?php
use PHPCurl\CurlWrapper\Curl;
use PHPCurl\CurlWrapper\CurlMulti;
$ch = new Curl("http://52.48.57.141/slow.php?num=10000&i=1");
$curlMulti = new CurlMulti();
$ch->setOpt(CURLOPT_RETURNTRANSFER, true);
$curlMulti->add($ch);
$done = false;
do {
$active = 0;
do {
$status = $curlMulti->exec(
$active
);
}
while($status === CURLM_CALL_MULTI_PERFORM);
if($active === 0) {
$done = true;
}
$content = $curlMulti->getContent($ch);
echo $content;
}
while($done === false);
echo "DONE!";
The above script outputs the following in chunks (truncated using ellipsis):
0
1
2
3
4
5
6
7
8
9
10
11
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
0
1
2
3
4
...
9998
9999
DONE!
As you can see, each time a chunk is received rather than just returning the chunk, the whole response is returned each time. This is observed by seeing the counter start from 0 multiple times.
I hope this is clear enough. It has been driving me mad, but at least I can isolate the issue. I hope it is something that I'm doing wrong - looking forward to hearing your thoughts.
@g105b I just released v3.0. Since you're on php 7.1, would you mind trying it out?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.