This library provides an object-oriented wrapper of the PHP cURL extension

Related tags

HTTP php curl
Overview

PHP Curl Class

This library provides an object-oriented wrapper of the PHP cURL extension.

Maintainability Test Coverage Total Downloads

If you have questions or problems with installation or usage create an Issue.

Installation

In order to install this library via composer run the following command in the console:

composer require curl/curl

or add the package manually to your composer.json file in the require section:

"curl/curl": "^2.0"

Usage examples

$curl = new Curl\Curl();
$curl->get('http://www.example.com/');
$curl = new Curl\Curl();
$curl->get('http://www.example.com/search', array(
    'q' => 'keyword',
));
$curl = new Curl\Curl();
$curl->post('http://www.example.com/login/', array(
    'username' => 'myusername',
    'password' => 'mypassword',
));
$curl = new Curl\Curl();
$curl->setBasicAuthentication('username', 'password');
$curl->setUserAgent('');
$curl->setReferrer('');
$curl->setHeader('X-Requested-With', 'XMLHttpRequest');
$curl->setCookie('key', 'value');
$curl->get('http://www.example.com/');

if ($curl->error) {
    echo $curl->error_code;
}
else {
    echo $curl->response;
}

var_dump($curl->request_headers);
var_dump($curl->response_headers);
$curl = new Curl\Curl();
$curl->setOpt(CURLOPT_RETURNTRANSFER, TRUE);
$curl->setOpt(CURLOPT_SSL_VERIFYPEER, FALSE);
$curl->get('https://encrypted.example.com/');
$curl = new Curl\Curl();
$curl->put('http://api.example.com/user/', array(
    'first_name' => 'Zach',
    'last_name' => 'Borboa',
));
$curl = new Curl\Curl();
$curl->patch('http://api.example.com/profile/', array(
    'image' => '@path/to/file.jpg',
));
$curl = new Curl\Curl();
$curl->delete('http://api.example.com/user/', array(
    'id' => '1234',
));
$curl->close();
// Example access to curl object.
curl_set_opt($curl->curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1');
curl_close($curl->curl);
// Example of downloading a file or any other content
$curl = new Curl\Curl();
// open the file where the request response should be written
$file_handle = fopen($target_file, 'w+');
// pass it to the curl resource
$curl->setOpt(CURLOPT_FILE, $file_handle);
// do any type of request
$curl->get('https://github.com');
// disable writing to file
$curl->setOpt(CURLOPT_FILE, null);
// close the file for writing
fclose($file_handle);

Testing

In order to test the library:

  1. Create a fork
  2. Clone the fork to your machine
  3. Install the depencies composer install
  4. Build and start the docker image (in tests/server) docker build . -t curlserver start docker run -p 1234:80 curlserver
  5. Run the unit tests ./vendor/bin/phpunit tests
Comments
  • json post

    json post

    In version 2, you force post data to be array. That prevents you from sanding raw json post requests. I guess that CURLOPT_POSTFIELDS can also take string values.

    discussion accepting pull request 
    opened by MichaelKubovic 8
  • PHP 8

    PHP 8

    @amouhzi We should change or remove php version restrictions, since php 8 has been released.

    https://github.com/php-mod/curl/blob/master/composer.json#L24

    opened by nadar 5
  • CurlFile fix

    CurlFile fix

    We are facing the same problem with CurlFile Uploads (https://github.com/php-mod/curl/issues/46).

    Please take a closer look at the fix in order to ensure its backwards compatible.

    opened by nadar 5
  • PUT does not send data as content body

    PUT does not send data as content body

    I am using this class to integrate my REST API, now I get to the problem, that my application expects the data, which gets sent through PUT, to update an ressource in the content-body of the Request. (Using Symfony2 HTTP Components to parse Request)

    As the RFC (http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html) indicates that the "enclosed entity" should be saved at the given URI, I would guess it should be within the Request body. It also states, that the "fundamental difference between the POST and PUT" methods are, that POST creates a new entity stored at the given endpoint and PUT updates the entity the URI points at.

    Last-but-not-least I will reference to http://www.restapitutorial.com/lessons/httpmethods.html which also states, that the entity should be within the request body.

    Am I wrong somewhere and Curl already does this in the background and the "fail" is on my side? As in the current implementation you append the data to the URI with http_build_query.

    opened by Blackskyliner 5
  • Added PHPDocs

    Added PHPDocs

    Improvments:

    • added basic phpdocs
    • udpate readme
    • add files to ignore list
    • used php-cs-fixer

    Changes:

    • marked setReferrer as deprecated but added setReferer

    Questions:

    • I was not able to make local testings, maybe adding a guide, or refractor the testing suite, as its seems a bit outdated.
    • Add version informations for deprecated methods, e.g. lets say will be removed in 2.0.0 and then removed them as well in version 2.0.0 ;-)

    closes #18

    opened by nadar 4
  • Cannot send boolean value

    Cannot send boolean value

    I passed following data to post method.

    array(4) {
      ["amount"]=>
      int(900)
      ["currency"]=>
      string(3) "JPY"
      ["card"]=>
      string(28) "tok_SMJhkoibbHr9CYsv2wwcZ1Ik"
      ["capture"]=>
      bool(false)
    }
    

    However the value of capture is changed from false to 0.

    string(67) "amount=900&currency=JPY&card=tok_SMJhkoibbHr9CYsv2wwcZ1Ik&capture=0"
    

    This is caused http_build_query inside of post method. https://github.com/php-mod/curl/blob/master/src/Curl/Curl.php#L60

    This case of scenario is missing in test cases.

    opened by matsubo 4
  • Verbose mode is not working

    Verbose mode is not working

    Verbose mode is not working because of $this->setOpt(CURLINFO_HEADER_OUT, true); in init function. even if you will try to set to false verbose will not work. you must not set it at all if you need verbose output.

    discussion accepting pull request 
    opened by jokaorgua 3
  • Post files does not work (php7.1)

    Post files does not work (php7.1)

    POSTing files does not work as intended.

    This is what I'd like to do:

    $file = new \CURLFile($filePath, mime_content_type($filePath), 'file_post_name');
    $data = array(
    	'mode' => 'import',
    	'file' => $file,
    );
    $this->curl->setHeader('Accept', 'application/json');
    $this->curl->setHeader('Content-Type', 'application/json');
    $this->curl->post('http://example.com', $data);
    $this->curl->close();
    

    However what is posted is a string representation of the data, including the CURLFile instance, not the actual file. I.e:

    mode=import&file%5Bname%5D=path/to/file/&file%5Bmime%5D=mime_type_of_file&file%5Bpostname%5D=file_post_name

    This is due to the preparePayload method - that runs http_build_query on the data. If I remove those lines everything works as expected.

    The library should include a way to skip http_build_query, or a special method for adding a file to the posted data.

    Thanks

    opened by danielsetreus 3
  • Add Travis badge to GitHub README

    Add Travis badge to GitHub README

    Nice to see this library is covered with Travis. Would it be possible to add a build badge to the README for this?

    Looks like some fixing is necessary anyway - PHP 7 and HHVM builds are broken presently.

    opened by halfer 3
  • Basic Usage Help

    Basic Usage Help

    I'm trying to figure out how to do this CURL command:

    curl https://uploads.stripe.com/v1/files
    -u sk_test_OCHANGEDPcDr0:
    -F purpose=identity_document
    -F file="@/path/to/a/file.jpg"

    With this PHP script? What would be the equivalent PHP command?

    opened by JordashTalon 3
  • Update README.md

    Update README.md

    Adding a short description to the readme of how to install the library. Otherwise people must open the composer.json or find the library on packagist to add them to the project.

    opened by nadar 3
  • Automatically set header in `prepareJsonPayload`

    Automatically set header in `prepareJsonPayload`

    I would suggest to automatically set header Content-type: application/json when calling prepareJsonPayload. Typically when doing post as json. What do you think? I could prepare PR for this change.

    accepting pull request 
    opened by mskocik 1
  • Error Message

    Error Message

    The library provides to many error message properties. They need either a better documentation or we should provide the user an error message function which returns the right message. The following error message properties are available:

    • curl_error_message
    • http_error_message
    • error_message
    • getErrorMessage()

    I assume that error_message contains the what users are looking for, either its a curl error or a http request error but there is also getErrorMessage() which is equals to curl_error_message.

    What could we do to improve the developer experience?

    discussion 
    opened by nadar 3
Releases(2.5.0)
Owner
PHP MOD
PHP MOD
Simple PHP curl wrapper class

php-curl The smallest possible OOP wrapper for PHP's curl capabilities. Note that this is not meant as a high-level abstraction. You should still know

Andreas Lutro 243 Dec 5, 2022
The HttpFoundation component defines an object-oriented layer for the HTTP specification.

HttpFoundation Component The HttpFoundation component defines an object-oriented layer for the HTTP specification. Resources Documentation Contributin

Symfony 8.3k Dec 29, 2022
The best php curl library.

中文文档 About Implemented by using php-curl internal io event with high performance,high universality,high extensibility which especially suitable for ma

Ares 431 Dec 12, 2022
Custom PHP curl library for the Laravel 5 framework - developed by Ixudra

ixudra/curl Custom PHP cURL library for the Laravel 4 or 5 framework - developed by Ixudra. The package provides an easy interface for sending cURL re

Jan Oris 556 Jan 6, 2023
The best php curl library.

中文文档 About Implemented by using php-curl internal io event with high performance,high universality,high extensibility which especially suitable for ma

Ares 431 Dec 12, 2022
PHP Curl ile letgo api kütüphanesi oluşturuldu. php ile letgo giriş yap.

Kendi LETGO API ile işlemler gerçekleştirelim. // email işlemleri $server = 'imap.gmail.com'; $user = '[email protected]'; $pass = 'password'; $port = 9

Görkem Bayraktar 2 Nov 3, 2022
A Chainable, REST Friendly, PHP HTTP Client. A sane alternative to cURL.

Httpful Httpful is a simple Http Client library for PHP 7.2+. There is an emphasis of readability, simplicity, and flexibility – basically provide the

Nate Good 1.7k Dec 21, 2022
PHP Curl Class makes it easy to send HTTP requests and integrate with web APIs

PHP Curl Class: HTTP requests made easy PHP Curl Class makes it easy to send HTTP requests and integrate with web APIs. Installation Requirements Quic

null 3.1k Jan 5, 2023
PHP cURL for feed Instagram Graph API

php-curl-instagram-graph PHP cURL for feed Instagram Graph API Script made based on the new (2020) Instagram API that requires authorization token gen

null 12 Apr 13, 2022
Simple HTTP cURL client for PHP 7.1+ based on PSR-18

Simple HTTP cURL client for PHP 7.1+ based on PSR-18 Installation composer require sunrise/http-client-curl QuickStart composer require sunrise/http-f

Sunrise // PHP 15 Sep 5, 2022
Plug & Play [CURL + Composer Optional], Proxy as a Service, Multi-tenant, Multi-Threaded, with Cache & Article Spinner

?? .yxorP The SAAS(y), Multitenancy & Augmenting Web Proxy Guzzler is a 100% SAAS(y) plug-and-play (PHP CURL+Composer are Optional) solution that leverages SAAS architecture to provide multi-tenancy, multiple threads, caching, and an article spinner service.

4D/ҵ.com Dashboards 12 Nov 17, 2022
Online tool to convert `curl` requests to Laravel `Http` requests

curl Converter Another bit of automation from Shift to convert curl requests to Laravel Http requests. This project is a WIP. You may follow along wit

Laravel Shift 66 Dec 17, 2022
A simple OOP wrapper to work with HTTP headers in PHP

Headers This package is to allow you to create HTTP Headers in PHP, in a simple and reliable way. Installation composer require http-php/headers Usage

null 5 Aug 9, 2022
The HttpClient component provides powerful methods to fetch HTTP resources synchronously or asynchronously.

HttpClient component The HttpClient component provides powerful methods to fetch HTTP resources synchronously or asynchronously. Resources Documentati

Symfony 1.7k Jan 6, 2023
This package provides the database factory experience to fake Http calls in your testsuite.

This package provides the database factory experience to fake Http calls in your testsuite

DIJ 11 May 2, 2022
Provides an easy interface for performing Hyper-Text Transfer Protocol (HTTP) requests

laminas-http provides the HTTP message abstraction used by laminas-mvc, and also provides an extensible, adapter-driven HTTP client library.

Laminas Project 33 Aug 27, 2022
Requests for PHP is a humble HTTP request library. It simplifies how you interact with other sites and takes away all your worries.

Requests for PHP Requests is a HTTP library written in PHP, for human beings. It is roughly based on the API from the excellent Requests Python librar

null 3.5k Dec 31, 2022
Requests for PHP is a humble HTTP request library. It simplifies how you interact with other sites and takes away all your worries.

Requests for PHP Requests is a HTTP library written in PHP, for human beings. It is roughly based on the API from the excellent Requests Python librar

null 3.5k Dec 31, 2022
Unirest in PHP: Simplified, lightweight HTTP client library.

Unirest for PHP Unirest is a set of lightweight HTTP libraries available in multiple languages, built and maintained by Mashape, who also maintain the

Kong 1.3k Dec 28, 2022