{"id":13409520,"url":"https://github.com/stil/curl-easy","last_synced_at":"2025-05-16T10:05:57.690Z","repository":{"id":3036971,"uuid":"4057554","full_name":"stil/curl-easy","owner":"stil","description":"cURL wrapper for PHP. Supports parallel and non-blocking requests. For high speed crawling, see stil/curl-robot","archived":false,"fork":false,"pushed_at":"2022-08-05T16:15:39.000Z","size":90,"stargazers_count":326,"open_issues_count":7,"forks_count":77,"subscribers_count":33,"default_branch":"master","last_synced_at":"2025-04-09T04:05:47.102Z","etag":null,"topics":["asynchronous","curl","http","multiprocessing","parallel","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stil.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-04-17T21:43:00.000Z","updated_at":"2025-01-28T01:07:07.000Z","dependencies_parsed_at":"2022-08-24T07:40:05.534Z","dependency_job_id":null,"html_url":"https://github.com/stil/curl-easy","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stil%2Fcurl-easy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stil%2Fcurl-easy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stil%2Fcurl-easy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stil%2Fcurl-easy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stil","download_url":"https://codeload.github.com/stil/curl-easy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254509476,"owners_count":22082891,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asynchronous","curl","http","multiprocessing","parallel","php"],"created_at":"2024-07-30T20:01:01.616Z","updated_at":"2025-05-16T10:05:57.673Z","avatar_url":"https://github.com/stil.png","language":"PHP","readme":"[![Travis](https://img.shields.io/travis/stil/curl-easy.svg)](https://travis-ci.org/stil/curl-easy)\n[![Latest Stable Version](https://poser.pugx.org/stil/curl-easy/v/stable)](https://packagist.org/packages/stil/curl-easy) [![Total Downloads](https://poser.pugx.org/stil/curl-easy/downloads)](https://packagist.org/packages/stil/curl-easy) [![License](https://poser.pugx.org/stil/curl-easy/license)](https://packagist.org/packages/stil/curl-easy)\n\n# Table of contents\n* [Introduction](#introduction)\n    * [Description](#description)\n\t* [Main Features](#main-features)\n* [Installation](#installation)\n* [Examples](#examples)\n* [cURL\\Request](#curlrequest)\n    * [Request::__construct](#request__construct)\n    * [Request::getOptions](#requestgetoptions)\n    * [Request::setOptions](#requestsetoptions)\n    * [Request::getContent](#requestgetcontent)\n    * [Request::getInfo](#requestgetinfo)\n    * [Request::send](#requestsend)\n* [cURL\\RequestQueue](#curlrequestqueue)\n    * [RequestsQueue::__construct](#requestsqueue__construct)\n    * [RequestsQueue::getDefaultOptions](#requestsqueuegetdefaultoptions)\n    * [RequestsQueue::setDefaultOptions](#requestsqueuesetdefaultoptions)\n    * [RequestsQueue::socketPerform](#requestsqueuesocketperform)\n    * [RequestsQueue::socketSelect](#requestsqueuesocketselect)\n    * [RequestsQueue::send](#requestsqueuesend)\n* [cURL\\Options](#curloptions)\n    * [Options::set](#optionsset)\n    * [Options::toArray](#optionstoarray)\n\n## Introduction\n### Description\nThis is small but powerful and robust library which speeds the things up. If you are tired of using PHP cURL extension with its procedural interface, but you want also keep control about script execution - it's great choice for you!\nIf you need high speed crawling in your project, you might be interested in stil/curl-easy extension - [stil/curl-robot](https://github.com/stil/curl-robot).\n### Main features\n* widely unit tested.\n* lightweight library with moderate level interface. It's not all-in-one library.\n* parallel/asynchronous connections with very simple interface.\n* attaching/detaching requests in parallel on run time!\n* support for callbacks, so you can control execution process.\n* intelligent setters as alternative to CURLOPT_* constants.\n* if you know the cURL php extension, you don't have to learn things from beginning\n\n## Installation\nIn order to use cURL-PHP library you need to install the » libcurl package.\n\nInstall this library as [Composer](http://getcomposer.org) package with following command:\n```bash\ncomposer require stil/curl-easy\n```\n## Examples\n### Single blocking request\n```php\n\u003c?php\n// We will check current Bitcoin price via API.\n$request = new \\cURL\\Request('https://bitpay.com/rates/USD');\n$request-\u003egetOptions()\n    -\u003eset(CURLOPT_TIMEOUT, 5)\n    -\u003eset(CURLOPT_RETURNTRANSFER, true);\n$response = $request-\u003esend();\n$feed = json_decode($response-\u003egetContent(), true);\necho \"Current Bitcoin price: \" . $feed['data']['rate'] . \" \" . $feed['data']['code'] . \"\\n\";\n```\nThe above example will output:\n```\nCurrent Bitcoin price: 1999.97 USD\n```\n\n### Single non-blocking request\n```php\n\u003c?php\n// We will check current Bitcoin price via API.\n$request = new \\cURL\\Request('https://bitpay.com/rates/USD');\n$request-\u003egetOptions()\n    -\u003eset(CURLOPT_TIMEOUT, 5)\n    -\u003eset(CURLOPT_RETURNTRANSFER, true);\n$request-\u003eaddListener('complete', function (\\cURL\\Event $event) {\n    $response = $event-\u003eresponse;\n    $feed = json_decode($response-\u003egetContent(), true);\n    echo \"\\nCurrent Bitcoin price: \" . $feed['data']['rate'] . \" \" . $feed['data']['code'] . \"\\n\";\n});\n\nwhile ($request-\u003esocketPerform()) {\n    usleep(1000);\n    echo '*';\n}\n```\nThe above example will output:\n```\n********************\nCurrent Bitcoin price: 1997.48 USD\n```\n\n### Requests in parallel\n```php\n\u003c?php\n// We will download Bitcoin rates for both USD and EUR in parallel.\n\n// Init requests queue.\n$queue = new \\cURL\\RequestsQueue;\n// Set default options for all requests in queue.\n$queue-\u003egetDefaultOptions()\n    -\u003eset(CURLOPT_TIMEOUT, 5)\n    -\u003eset(CURLOPT_RETURNTRANSFER, true);\n// Set function to execute when request is complete.\n$queue-\u003eaddListener('complete', function (\\cURL\\Event $event) {\n    $response = $event-\u003eresponse;\n    $json = $response-\u003egetContent(); // Returns content of response\n    $feed = json_decode($json, true);\n    echo \"Current Bitcoin price: \" . $feed['data']['rate'] . \" \" . $feed['data']['code'] . \"\\n\";\n});\n\n$request = new \\cURL\\Request('https://bitpay.com/rates/USD');\n// Add request to queue\n$queue-\u003eattach($request);\n\n$request = new \\cURL\\Request('https://bitpay.com/rates/EUR');\n$queue-\u003eattach($request);\n\n// Execute queue\n$timeStart = microtime(true);\n$queue-\u003esend();\n$elapsedMs = (microtime(true) - $timeStart) * 1000;\necho 'Elapsed time: ' . round($elapsedMs) . \" ms\\n\";\n```\nThe above example will output:\n```\nCurrent Bitcoin price: 1772.850062 EUR\nCurrent Bitcoin price: 1987.01 USD\nElapsed time: 284 ms\n```\n\n### Non-blocking requests in parallel\n```php\n\u003c?php\n// We will download Bitcoin rates for both USD and EUR in parallel.\n\n// Init requests queue.\n$queue = new \\cURL\\RequestsQueue;\n// Set default options for all requests in queue.\n$queue-\u003egetDefaultOptions()\n    -\u003eset(CURLOPT_TIMEOUT, 5)\n    -\u003eset(CURLOPT_RETURNTRANSFER, true);\n// Set function to execute when request is complete.\n$queue-\u003eaddListener('complete', function (\\cURL\\Event $event) {\n    $response = $event-\u003eresponse;\n    $json = $response-\u003egetContent(); // Returns content of response\n    $feed = json_decode($json, true);\n    echo \"\\nCurrent Bitcoin price: \" . $feed['data']['rate'] . \" \" . $feed['data']['code'] . \"\\n\";\n});\n\n$request = new \\cURL\\Request('https://bitpay.com/rates/USD');\n// Add request to queue\n$queue-\u003eattach($request);\n\n$request = new \\cURL\\Request('https://bitpay.com/rates/EUR');\n$queue-\u003eattach($request);\n\n// Execute queue\n$timeStart = microtime(true);\nwhile ($queue-\u003esocketPerform()) {\n    usleep(1000);\n    echo '*';\n}\n$elapsedMs = (microtime(true) - $timeStart) * 1000;\necho 'Elapsed time: ' . round($elapsedMs) . \" ms\\n\";\n```\nThe above example will output something like that:\n```\n*****************************************************************************************************************************************************\nCurrent Bitcoin price: 1772.145208 EUR\n************************************************************************\nCurrent Bitcoin price: 1986.22 USD\nElapsed time: 374 ms\n```\n\n### Processing queue of multiple requests while having maximum 2 at once executed at the moment\n```php\n$requests = [];\n$currencies = ['USD', 'EUR', 'JPY', 'CNY'];\nforeach ($currencies as $code) {\n    $requests[] = new \\cURL\\Request('https://bitpay.com/rates/' . $code);\n}\n\n$queue = new \\cURL\\RequestsQueue;\n$queue\n    -\u003egetDefaultOptions()\n    -\u003eset(CURLOPT_RETURNTRANSFER, true);\n\n$queue-\u003eaddListener('complete', function (\\cURL\\Event $event) use (\u0026$requests) {\n    $response = $event-\u003eresponse;\n    $json = $response-\u003egetContent(); // Returns content of response\n    $feed = json_decode($json, true);\n    echo \"Current Bitcoin price: \" . $feed['data']['rate'] . \" \" . $feed['data']['code'] . \"\\n\";\n\n    if ($next = array_pop($requests)) {\n        $event-\u003equeue-\u003eattach($next);\n    }\n});\n\n$queue-\u003eattach(array_pop($requests));\n$queue-\u003eattach(array_pop($requests));\n$queue-\u003esend();\n```\nThe above example will output something like that:\n```\nCurrent Bitcoin price: 220861.025 JPY\nCurrent Bitcoin price: 13667.81675 CNY\nCurrent Bitcoin price: 1771.0567 EUR\nCurrent Bitcoin price: 1985 USD\n```\n### Intelligent Options setting\nReplace `CURLOPT_*` with `set*()` and you will receive method name.\nExamples:\n```php\n$opts = new \\cURL\\Options;\n\n$opts-\u003eset(CURLOPT_URL, $url);\n// it is equivalent to\n// $opts-\u003esetUrl($url);\n\n$opts-\u003eset(CURLOPT_RETURNTRANSFER, true);\n// it is equivalent to\n// $opts-\u003esetReturnTransfer(true);\n// or\n// $opts-\u003esetReTURNTranSFER(true);\n// character case does not matter\n\n$opts-\u003eset(CURLOPT_TIMEOUT, 5);\n// it is equivalent to\n// $opts-\u003esetTimeout(5);\n\n// then you can assign options to Request\n\n$request = new \\cURL\\Request;\n$request-\u003esetOptions($opts);\n\n// or make it default in RequestsQueue\n\n$queue = new \\cURL\\RequestsQueue;\n$queue-\u003esetDefaultOptions($opts);\n```\n### Error handling\nYou can access cURL error codes in Response class.\nExamples:\n```php\n$request = new \\cURL\\Request('http://non-existsing-page/');\n$response = $request-\u003esend();\n\nif ($response-\u003ehasError()) {\n    $error = $response-\u003egetError();\n    echo 'Error code: ' . $error-\u003egetCode() . \"\\n\";\n    echo 'Message: \"' . $error-\u003egetMessage() . '\"' . \"\\n\";\n}\n```\nProbably above example will output\n```\nError code: 6\nMessage: \"Could not resolve host: non-existsing-page; Host not found\"\n```\nYou can find all of CURLE_* error codes [here](http://php.net/manual/en/curl.constants.php).\n## cURL\\Request\n### Request::__construct\n### Request::getOptions\n### Request::setOptions\n### RequestsQueue::socketPerform\n### RequestsQueue::socketSelect\n### Request::send\n## cURL\\RequestQueue\n### RequestsQueue::__construct\n### RequestsQueue::getDefaultOptions\n### RequestsQueue::setDefaultOptions\n### RequestsQueue::socketPerform\n### RequestsQueue::socketSelect\n### RequestsQueue::send\n## cURL\\Response\n### Response::getContent\n### Response::getInfo\n### Response::hasError\n### Response::getError\n## cURL\\Options\n### Options::set\n### Options::toArray\n## cURL\\Error\n### Error::getCode\n### Error::getMessage\n","funding_links":[],"categories":["PHP"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstil%2Fcurl-easy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstil%2Fcurl-easy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstil%2Fcurl-easy/lists"}