X-Git-Url: https://git.cweiske.de/shpub.git/blobdiff_plain/6f748120fbb71c4a7b3d9fd762f40933b8cb4e95..059c6ab77b8e63210b9afc266d62c9f543d4d7ae:/src/shpub/Request.php diff --git a/src/shpub/Request.php b/src/shpub/Request.php index cbcc57e..0cadc48 100644 --- a/src/shpub/Request.php +++ b/src/shpub/Request.php @@ -6,9 +6,15 @@ class Request public $req; public $cfg; + protected $sendAsJson = false; protected $uploadsInfo = []; protected $dedicatedBody = false; + protected $properties = []; + protected $type = null; + protected $action = null; + protected $url = null; + public function __construct($host, $cfg) { $this->cfg = $cfg; @@ -25,6 +31,49 @@ class Request public function send($body = null) { + if ($this->sendAsJson) { + //application/json + if ($body !== null) { + throw new \Exception('body already defined'); + } + $this->req->setHeader('Content-Type: application/json'); + $data = []; + if ($this->action !== null) { + $data['action'] = $this->action; + } + if ($this->url !== null) { + $data['url'] = $this->url; + } + if ($this->type !== null) { + $data['type'] = 'h-' . $this->type; + } + if (count($this->properties)) { + $data['properties'] = $this->properties; + } + $body = json_encode($data); + } else { + //form-encoded + if ($this->type !== null) { + $this->req->addPostParameter('h', $this->type); + } + if ($this->action !== null) { + $this->req->addPostParameter('action', $this->action); + } + if ($this->url !== null) { + $this->req->addPostParameter('url', $this->url); + } + foreach ($this->properties as $propkey => $propval) { + if (isset($propval['html'])) { + //workaround for content[html] + $propkey = $propkey . '[html]'; + $propval = $propval['html']; + } else if (count($propval) == 1) { + $propval = reset($propval); + } + $this->req->addPostParameter($propkey, $propval); + } + } + if ($body !== null) { $this->dedicatedBody = true; $this->req->setBody($body); @@ -44,30 +93,44 @@ class Request return $res; } + public function setAction($action) + { + $this->action = $action; + } + + public function setType($type) + { + $this->type = $type; + } + + public function setUrl($url) + { + $this->url = $url; + } + /** - * @param string $fieldName name of file-upload field - * @param string|resource|array $filename full name of local file, + * @param string $fieldName name of file-upload field + * @param string|resource|array $filename full name of local file, * pointer to open file or an array of files */ public function addUpload($fieldName, $filename) { + if ($this->sendAsJson) { + throw new \Exception('File uploads do not work with JSON'); + } $this->uploadsInfo[$fieldName] = $filename; return $this->req->addUpload($fieldName, $filename); } /** - * Add one or multiple POST parameters. - * Automatically adds them as array or as string. + * Adds a micropub property to the request. * * @param string $key Parameter name * @param string|array $values One or multiple values */ - public function addPostParameter($key, $values) + public function addProperty($key, $values) { - if (count($values) == 1) { - $values = reset($values); - } - $this->req->addPostParameter($key, $values); + $this->properties[$key] = (array) $values; } protected function printCurl() @@ -122,4 +185,9 @@ class Request Log::msg($command); } + + public function setSendAsJson($json) + { + $this->sendAsJson = $json; + } } \ No newline at end of file