Fix JSON HTML content sending, according to new tests
[shpub.git] / src / shpub / CurlPrinter.php
1 <?php
2 namespace shpub;
3
4 class CurlPrinter
5 {
6     public function show($httpReq, $uploadsInfo = [], $dedicatedBody = false)
7     {
8         $command = 'curl';
9         if ($httpReq->getMethod() != 'GET') {
10             $command .= ' -X ' . $httpReq->getMethod();
11         }
12         foreach ($httpReq->getHeaders() as $key => $val) {
13             $caseKey = implode('-', array_map('ucfirst', explode('-', $key)));
14             $command .= ' -H ' . escapeshellarg($caseKey . ': ' . $val);
15         }
16
17         $postParams = $httpReq->getPostParams();
18
19         if (count($uploadsInfo) == 0) {
20             foreach ($postParams as $k => $v) {
21                 if (!is_array($v)) {
22                     $command .= ' -d ' . escapeshellarg($k . '=' . $v);
23                 } else {
24                     foreach ($v as $ak => $av) {
25                         $command .= ' -d ' . escapeshellarg(
26                             $k . '[' . $ak . ']=' . $av
27                         );
28                     }
29                 }
30             }
31         } else {
32             foreach ($postParams as $k => $v) {
33                 $command .= ' -F ' . escapeshellarg($k . '=' . $v);
34             }
35             foreach ($uploadsInfo as $fieldName => $fileName) {
36                 if (!is_array($fileName)) {
37                     $command .= ' -F ' . escapeshellarg(
38                         $fieldName . '=@' . $fileName
39                     );
40                 } else {
41                     foreach ($fileName as $k => $realFilename) {
42                         $command .= ' -F ' . escapeshellarg(
43                             $fieldName . '[' . $k . ']=@' . $realFilename
44                         );
45                     }
46                 }
47             }
48         }
49
50         if ($dedicatedBody) {
51             $command .= ' --data ' . escapeshellarg($httpReq->getBody());
52         }
53
54         $command .= ' ' . escapeshellarg((string) $httpReq->getUrl());
55
56         Log::msg($command);
57     }
58 }
59 ?>