Add support for update queries
[shpub.git] / src / shpub / Request.php
index 44894c572124a5662042145855eb7d9283129520..cbcc57e0e4a8e4fe348139ab850c78899dc53667 100644 (file)
@@ -7,6 +7,7 @@ class Request
     public $cfg;
 
     protected $uploadsInfo = [];
+    protected $dedicatedBody = false;
 
     public function __construct($host, $cfg)
     {
@@ -25,6 +26,7 @@ class Request
     public function send($body = null)
     {
         if ($body !== null) {
+            $this->dedicatedBody = true;
             $this->req->setBody($body);
         }
         if ($this->cfg->debug) {
@@ -53,6 +55,21 @@ class Request
         return $this->req->addUpload($fieldName, $filename);
     }
 
+    /**
+     * Add one or multiple POST parameters.
+     * Automatically adds them as array or as string.
+     *
+     * @param string       $key    Parameter name
+     * @param string|array $values One or multiple values
+     */
+    public function addPostParameter($key, $values)
+    {
+        if (count($values) == 1) {
+            $values = reset($values);
+        }
+        $this->req->addPostParameter($key, $values);
+    }
+
     protected function printCurl()
     {
         $command = 'curl';
@@ -68,7 +85,15 @@ class Request
 
         if (count($this->uploadsInfo) == 0) {
             foreach ($postParams as $k => $v) {
-                $command .= ' -d ' . escapeshellarg($k . '=' . $v);
+                if (!is_array($v)) {
+                    $command .= ' -d ' . escapeshellarg($k . '=' . $v);
+                } else {
+                    foreach ($v as $ak => $av) {
+                        $command .= ' -d ' . escapeshellarg(
+                            $k . '[' . $ak . ']=' . $av
+                        );
+                    }
+                }
             }
         } else {
             foreach ($postParams as $k => $v) {
@@ -89,8 +114,12 @@ class Request
             }
         }
 
+        if ($this->dedicatedBody) {
+            $command .= ' --data ' . escapeshellarg($this->req->getBody());
+        }
+
         $command .= ' ' . escapeshellarg((string) $this->req->getUrl());
 
-        echo $command . "\n";
+        Log::msg($command);
     }
 }
\ No newline at end of file