Giter Site home page Giter Site logo

phpcurl / curlwrapper Goto Github PK

View Code? Open in Web Editor NEW
11.0 3.0 5.0 50 KB

The simplest OOP cURL wrapper for PHP

Home Page: https://packagist.org/packages/phpcurl/curlwrapper

License: MIT License

PHP 100.00%
curl curlwrapper curl-multi oop injectable dependency-injection

curlwrapper's People

Contributors

f3ath avatar slax0rr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

curlwrapper's Issues

CurlMulti::getContent does not seem to match curl_multi_get_content behaviour

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.

Example using native functions:

<?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!

Example using CurlWrapper:

<?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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.