Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jensostertag/curl-adapter
Wrapper Library for PHP's cURL Functions that allows to easily send GET and POST Requests
https://github.com/jensostertag/curl-adapter
curl php
Last synced: 3 months ago
JSON representation
Wrapper Library for PHP's cURL Functions that allows to easily send GET and POST Requests
- Host: GitHub
- URL: https://github.com/jensostertag/curl-adapter
- Owner: JensOstertag
- License: mit
- Created: 2023-06-24T11:20:14.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-06-24T20:53:43.000Z (7 months ago)
- Last Synced: 2024-09-26T00:24:48.533Z (3 months ago)
- Topics: curl, php
- Language: PHP
- Homepage:
- Size: 33.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-MIT
Awesome Lists containing this project
README
# Curl-Adapter for PHP
This is a wrapper library for PHP's cURL functions. It allows you to easily send GET and POST requests## Installation
To install this library, include it in your project using composer:
```json
{
"require": {
"jensostertag/curl-adapter": "~1.1.0"
}
}
```## Usage
Simple GET or POST requests
The following example shows how to send a GET request to a HTML page:
```php
$curl = new Curl();
$curl->setUrl("URL");
$curl->setMethod(Curl::$METHOD_GET);
$curl->setHeaders([
"Accept: text/html, application/xhtml+xml"
]);
$response = $curl->execute();
$responseCode = $curl->getHttpCode();
$curl->close();
```
`URL` is the URL of the server that you want to send the request to.To send a POST request, simply replace `Curl::$METHOD_GET` with `Curl::$METHOD_POST`. However note, that the above example does not send any POST data to the server.
POST requests with data
To send POST data to the server, use the `setPostFields()` method:
```php
$curl = new Curl();
$curl->setUrl("URL");
$curl->setMethod(Curl::$METHOD_POST);
$curl->setHeaders([
"Accept: application/json"
]);
$curl->setPostFields([
"key" => "value"
]);
$response = $curl->execute();
$responseCode = $curl->getHttpCode();
$curl->close();
```
The above example requests a JSON response from the server with the URL `URL` and sends the POST data `key=value` along with the request.You can also send JSON-encoded POST data by setting the `asJson` flag of the `setPostFields` method to `true`.