Skip to content

Commit 270bb32

Browse files
authored
Merge pull request #1 from SergeyBel/add-readme
upgrade README
2 parents 8955889 + 9b945f3 commit 270bb32

File tree

7 files changed

+96
-14
lines changed

7 files changed

+96
-14
lines changed

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ language: php
22

33
php:
44
- 7.1
5+
- 7.2
6+
- 7.4
57

68
before_script:
79
- composer install
810

911
script:
10-
- vendor/bin/php-cs-fixer src --dry-run
12+
- vendor/bin/php-cs-fixer fix src --dry-run
1113
- vendor/bin/phpunit tests --colors --coverage-clover=coverage.xml

README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,34 @@
1-
# curl-printer
1+
# curl-printer [![Build Status](https://travis-ci.com/SergeyBel/curl-printer.svg?branch=main)](https://travis-ci.com/SergeyBel/curl-printer)
2+
3+
**curl-printer** is a library which allows you print php PSR-7 request as curl command line string. It is useful for logging and debugging
4+
5+
## Usage
6+
7+
```php
8+
use CurlPrinter\CurlPrinter;
9+
use GuzzleHttp\Psr7\Request;
10+
11+
$request = new Request(
12+
'POST',
13+
'https://someapi.com/v2/user/create',
14+
[
15+
'Accept' => 'application/json',
16+
],
17+
'user_id=12345'
18+
);
19+
$printer = new CurlPrinter();
20+
echo $printer->printRequest($request);
21+
// curl -X POST https://someapi.com/v2/user/create -d 'user_id=12345' -H 'Accept: application/json'
22+
```
23+
24+
## Guzzle middleware
25+
You can use **CurlPrinterMiddleware** for comfortable work with Guzzle (see [examples](https://github.com/SergeyBel/curl-printer/tree/main/examples))
26+
27+
```php
28+
$logger = // some LoggerInterface
29+
30+
$stack = // Guzzle handler stack
31+
$stack->push(new CurlPrinterMiddleware($logger));
32+
$client = new Client(['handler' => $stack]);
33+
$client->post(...);
34+
```

composer.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@
1313
}
1414
},
1515

16+
"require": {
17+
"php": ">=7.1"
18+
},
19+
1620
"require-dev": {
17-
"guzzlehttp/guzzle": "7.0",
18-
"monolog/monolog": "^2.1",
19-
"phpunit/phpunit": "9",
21+
"guzzlehttp/guzzle": ">=6.0",
22+
"monolog/monolog": ">=1.1",
23+
"phpunit/phpunit": ">=7",
2024
"friendsofphp/php-cs-fixer": "^2.16"
2125
}
2226
}

examples/get.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use \GuzzleHttp\HandlerStack;
99

1010

11-
require_once '../vendor/autoload.php';
11+
require_once __DIR__ . '/../vendor/autoload.php';
1212

1313

1414
$logger = new Logger('example.logger');

examples/post_json.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use CurlPrinter\CurlPrinterMiddleware;
4+
use GuzzleHttp\Client;
5+
use GuzzleHttp\Handler\CurlHandler;
6+
use \Monolog\Logger;
7+
use \Monolog\Handler\StreamHandler;
8+
use \GuzzleHttp\HandlerStack;
9+
10+
11+
require_once __DIR__ . '/../vendor/autoload.php';
12+
13+
14+
$logger = new Logger('example.logger');
15+
$logger->pushHandler(new StreamHandler('php://stdout'));
16+
17+
18+
$stack = HandlerStack::create();
19+
$stack->setHandler(new CurlHandler());
20+
$stack->push(new CurlPrinterMiddleware($logger));
21+
$client = new Client(['handler' => $stack]);
22+
$client->post('https://www.google.com/',
23+
[
24+
'json' => [
25+
"key" => "value",
26+
],
27+
'headers' => [
28+
'Content-type' => 'application/json; charset=utf-8',
29+
'Accept' => 'application/json',
30+
],
31+
]
32+
);
33+

src/CurlFormatter.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22
namespace CurlPrinter;
33

4-
54
/**
65
* Class CurlFormatter
76
* Format CurlData to curl command line string
@@ -48,7 +47,7 @@ private function addBody(CurlData $curlData): void
4847
{
4948
$body = $curlData->getBody();
5049
if (!empty($body)) {
51-
$this->addNamedOption(CurlOptions::OPTION_BODY, '"'.$curlData->getBody().'"');
50+
$this->addNamedOption(CurlOptions::OPTION_BODY, "'".$curlData->getBody()."'");
5251
}
5352
}
5453

@@ -58,7 +57,7 @@ private function addHeaders(CurlData $curlData): void
5857

5958
foreach ($headers as $name => $header) {
6059
if (!in_array($name, $this->skippedHeaders)) {
61-
$this->addNamedOption(CurlOptions::OPTION_HEADER, '"'.$name.': '.$header[0].'"');
60+
$this->addNamedOption(CurlOptions::OPTION_HEADER, "'".$name.': '.$header[0]."'");
6261
}
6362
}
6463
}

tests/CurlPrinterTest.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,28 @@ protected function setUp(): void
1818
public function testSimpleGet()
1919
{
2020
$request = $this->createRequest('GET');
21-
$answer = 'curl http://test.tst';
21+
$answer = "curl http://test.tst";
2222
$this->assertSame($answer, $this->printer->printRequest($request));
2323
}
2424

2525
public function testGetWithParams()
2626
{
2727
$request = $this->createRequest('GET', [], '', 'http://test.tst?param1=value1');
28-
$answer = 'curl http://test.tst?param1=value1';
28+
$answer = "curl http://test.tst?param1=value1";
2929
$this->assertSame($answer, $this->printer->printRequest($request));
3030
}
3131

3232
public function testSimplePost()
3333
{
3434
$request = $this->createRequest('Post');
35-
$answer = 'curl -X POST http://test.tst';
35+
$answer = "curl -X POST http://test.tst";
3636
$this->assertSame($answer, $this->printer->printRequest($request));
3737
}
3838

3939
public function testPostWithBody()
4040
{
4141
$request = $this->createRequest('POST', [], 'param1=value1&param2=value2');
42-
$answer = 'curl -X POST http://test.tst -d "param1=value1&param2=value2"';
42+
$answer = "curl -X POST http://test.tst -d 'param1=value1&param2=value2'";
4343
$this->assertSame($answer, $this->printer->printRequest($request));
4444
}
4545

@@ -51,7 +51,18 @@ public function testGetWithHeaders()
5151
'Accept' => 'application/json',
5252
'Content-Type' => 'application/xml'
5353
]);
54-
$answer = 'curl http://test.tst -H "Accept: application/json" -H "Content-Type: application/xml"';
54+
$answer = "curl http://test.tst -H 'Accept: application/json' -H 'Content-Type: application/xml'";
55+
$this->assertSame($answer, $this->printer->printRequest($request));
56+
}
57+
58+
public function testPostJsonBody()
59+
{
60+
$request = $this->createRequest(
61+
'POST',
62+
[],
63+
json_encode(['key' => 'value'])
64+
);
65+
$answer = "curl -X POST http://test.tst -d '{\"key\":\"value\"}'";
5566
$this->assertSame($answer, $this->printer->printRequest($request));
5667
}
5768

0 commit comments

Comments
 (0)