Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ env:
DEFAULT_COMPOSER_FLAGS: "--prefer-dist --no-interaction"
CC_TEST_REPORTER_ID: 40d4890deed3bca8888c04ca67b9768edf11d7a089d2960977997791daea31f6
jobs:
## PHPUNIT
phpunit:
name: PHP ${{ matrix.php }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
Expand All @@ -15,7 +16,7 @@ jobs:
os: [ubuntu-latest]
php: ['5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1']
steps:
## checkout the repoistory §
## checkout the repoistory
- name: Checkout Repo
uses: actions/checkout@v2

Expand All @@ -26,7 +27,7 @@ jobs:
- name: Run test server
run: docker run -d -p 1234:80 localserver

## Install php
## Install PHP
- name: Install PHP
uses: shivammathur/setup-php@v2
with:
Expand Down Expand Up @@ -56,4 +57,4 @@ jobs:
chmod +x ./cc-test-reporter
./cc-test-reporter after-build -t clover
if: matrix.php == '7.1'
continue-on-error: true # if is fork
continue-on-error: true # if is fork
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ composer.lock
tests/phpunit_report/*
/.settings/
/.php_cs.cache
.php-cs-fixer.cache
15 changes: 15 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

$finder = PhpCsFixer\Finder::create()
->in(__DIR__)
;

$config = new PhpCsFixer\Config();
return $config->setRules([
'@PSR12' => true,
'ordered_imports' => ['sort_algorithm' => 'alpha'],
'no_unused_imports' => true,
'visibility_required' => ['elements' => ['property', 'method']]
])
->setFinder($finder)
;
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/).

## 2.x

+ [#93](https://github.com/php-mod/curl/pull/93) Text and Coding Standards update

## 2.4.0 (29. August 2022)

+ [#84](https://github.com/php-mod/curl/pull/84) Added `.gitattributes`.
Expand Down
65 changes: 26 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,87 +27,75 @@ or add the package manually to your composer.json file in the require section:

## Usage examples

A few example for using CURL with get:

```php
$curl = new Curl\Curl();
$curl->get('http://www.example.com/');
$curl = (new Curl\Curl())->get('http://www.example.com/');
if ($curl->isSuccess()) {
// do something with response
var_dump($curl->response);
}
// ensure to close the curl connection
$curl->close();
```

Or with params, values will be encoded with `PHP_QUERY_RFC1738`:

```php
$curl = new Curl\Curl();
$curl->get('http://www.example.com/search', array(
$curl = (new Curl\Curl())->get('http://www.example.com/search', [
'q' => 'keyword',
));
]);
```

An example using post

```php
$curl = new Curl\Curl();
$curl->post('http://www.example.com/login/', array(
$curl->post('http://www.example.com/login/', [
'username' => 'myusername',
'password' => 'mypassword',
));
]);
```

An exampling using basic authentication, remove default user agent and working with error handling

```php
$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 {
} else {
echo $curl->response;
}

var_dump($curl->request_headers);
var_dump($curl->response_headers);
```

SSL verification setup:

```php
$curl = new Curl\Curl();
$curl->setOpt(CURLOPT_RETURNTRANSFER, TRUE);
$curl->setOpt(CURLOPT_SSL_VERIFYPEER, FALSE);
$curl->get('https://encrypted.example.com/');
```

```php
$curl = new Curl\Curl();
$curl->put('http://api.example.com/user/', array(
'first_name' => 'Zach',
'last_name' => 'Borboa',
));
```

```php
$curl = new Curl\Curl();
$curl->patch('http://api.example.com/profile/', array(
'image' => '@path/to/file.jpg',
));
```

```php
$curl = new Curl\Curl();
$curl->delete('http://api.example.com/user/', array(
'id' => '1234',
));
```

```php
$curl->close();
```
Example access to curl object:

```php
// 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

```php
// 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+');
Expand All @@ -121,7 +109,6 @@ $curl->setOpt(CURLOPT_FILE, null);
fclose($file_handle);
```


## Testing

In order to test the library:
Expand All @@ -130,4 +117,4 @@ In order to test the library:
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`
4. Run the unit tests `./vendor/bin/phpunit tests`
5. Run the unit tests `./vendor/bin/phpunit tests`
24 changes: 12 additions & 12 deletions src/Curl/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public function addResponseHeaderLine($curl, $header_line)
} elseif (!$this->response_header_continue) {
$this->response_headers[] = $trimmed_header;
}

return strlen($header_line);
}

Expand Down Expand Up @@ -241,7 +241,7 @@ protected function preparePayload($data)
$skip = true;
}
}

if (!$skip) {
$data = http_build_query($data);
}
Expand Down Expand Up @@ -318,9 +318,9 @@ public function get($url, $data = array())
/**
* Purge Request
*
* A very common scenario to send a purge request is within the use of varnish, therefore
* A very common scenario to send a purge request is within the use of varnish, therefore
* the optional hostname can be defined.
*
*
* @param strng $url The url to make the purge request
* @param string $hostname An optional hostname which will be sent as http host header
* @return self
Expand All @@ -329,14 +329,14 @@ public function get($url, $data = array())
public function purge($url, $hostName = null)
{
$this->setOpt(CURLOPT_URL, $url);
$this->setOpt(CURLOPT_CUSTOMREQUEST, 'PURGE');
$this->setOpt(CURLOPT_CUSTOMREQUEST, 'PURGE');
if ($hostName) {
$this->setHeader('Host', $hostName);
}
$this->exec();
return $this;
}

/**
* Make a post request with optional post data.
*
Expand Down Expand Up @@ -581,7 +581,7 @@ public function getOpt($option)
{
return curl_getinfo($this->curl, $option);
}

/**
* Return the endpoint set for curl
*
Expand Down Expand Up @@ -717,7 +717,7 @@ public function isServerError()
{
return $this->getHttpStatus() >= 500 && $this->getHttpStatus() < 600;
}

/**
* Get a specific response header key or all values from the response headers array.
*
Expand Down Expand Up @@ -750,17 +750,17 @@ public function getResponseHeaders($headerKey = null)

foreach ($this->response_headers as $header) {
$parts = explode(":", $header, 2);

$key = isset($parts[0]) ? $parts[0] : '';
$value = isset($parts[1]) ? $parts[1] : '';

$headers[trim(strtolower($key))] = trim($value);
}

if ($headerKey) {
return isset($headers[$headerKey]) ? $headers[$headerKey] : false;
}

return $headers;
}

Expand Down
6 changes: 3 additions & 3 deletions tests/CurlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public function testReferrer()
'key' => 'HTTP_REFERER',
)) === 'myreferrer');
}

public function testDeprecatedReferrer()
{
$this->curl->setReferrer('myreferrer');
Expand Down Expand Up @@ -273,7 +273,7 @@ public function testHeaders()
public function testHeadersWithContinue()
{
$headers = file(dirname(__FILE__) . '/data/response_headers_with_continue.txt');

$this->curl->response_headers = array();
foreach ($headers as $header_line) {
$this->curl->addResponseHeaderLine(null, $header_line);
Expand All @@ -285,7 +285,7 @@ public function testHeadersWithContinue()

$this->assertEquals($expected_headers, $this->curl->response_headers);
}

public function testReset()
{
$curl = $this->getMockBuilder(get_class($this->curl))->getMock();
Expand Down
1 change: 1 addition & 0 deletions tests/server/php-curl-test/server.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

$request_method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : '';
$data_values = $request_method === 'POST' ? $_POST : $_GET;
$test = isset($data_values['test']) ? $data_values['test'] : '';
Expand Down