9 protected $sendAsJson = false;
10 protected $uploadsInfo = [];
11 protected $dedicatedBody = false;
13 protected $properties = [];
14 protected $type = null;
15 protected $action = null;
16 protected $url = null;
18 public function __construct($host, $cfg)
21 $this->req = new MyHttpRequest2($host->endpoints->micropub, 'POST');
22 $this->req->setHeader('User-Agent: shpub');
23 if (version_compare(PHP_VERSION, '5.6.0', '<')) {
24 //correct ssl validation on php 5.5 is a pain, so disable
25 $this->req->setConfig('ssl_verify_host', false);
26 $this->req->setConfig('ssl_verify_peer', false);
28 $this->req->setHeader('Content-type', 'application/x-www-form-urlencoded');
29 $this->req->setHeader('authorization', 'Bearer ' . $host->token);
32 public function send($body = null)
34 if ($this->sendAsJson) {
37 throw new \Exception('body already defined');
39 $this->req->setHeader('Content-Type: application/json');
41 if ($this->action !== null) {
42 $data['action'] = $this->action;
44 if ($this->url !== null) {
45 $data['url'] = $this->url;
47 if ($this->type !== null) {
48 $data['type'] = 'h-' . $this->type;
50 if (count($this->properties)) {
51 $data['properties'] = $this->properties;
53 $body = json_encode($data);
56 if ($this->type !== null) {
57 $this->req->addPostParameter('h', $this->type);
59 if ($this->action !== null) {
60 $this->req->addPostParameter('action', $this->action);
62 if ($this->url !== null) {
63 $this->req->addPostParameter('url', $this->url);
65 foreach ($this->properties as $propkey => $propval) {
66 if (isset($propval['html'])) {
67 //workaround for content[html]
68 $propkey = $propkey . '[html]';
69 $propval = $propval['html'];
70 } else if (count($propval) == 1) {
71 $propval = reset($propval);
73 $this->req->addPostParameter($propkey, $propval);
78 $this->dedicatedBody = true;
79 $this->req->setBody($body);
81 if ($this->cfg->debug) {
84 $res = $this->req->send();
86 if (intval($res->getStatus() / 100) != 2) {
88 'Server returned an error status code ' . $res->getStatus()
90 Log::err($res->getBody());
96 public function setAction($action)
98 $this->action = $action;
101 public function setType($type)
106 public function setUrl($url)
112 * @param string $fieldName name of file-upload field
113 * @param string|resource|array $filename full name of local file,
114 * pointer to open file or an array of files
116 public function addUpload($fieldName, $filename)
118 if ($this->sendAsJson) {
119 throw new \Exception('File uploads do not work with JSON');
121 $this->uploadsInfo[$fieldName] = $filename;
122 return $this->req->addUpload($fieldName, $filename);
126 * Adds a micropub property to the request.
128 * @param string $key Parameter name
129 * @param string|array $values One or multiple values
131 public function addProperty($key, $values)
133 $this->properties[$key] = (array) $values;
136 protected function printCurl()
139 if ($this->req->getMethod() != 'GET') {
140 $command .= ' -X ' . $this->req->getMethod();
142 foreach ($this->req->getHeaders() as $key => $val) {
143 $caseKey = implode('-', array_map('ucfirst', explode('-', $key)));
144 $command .= ' -H ' . escapeshellarg($caseKey . ': ' . $val);
147 $postParams = $this->req->getPostParams();
149 if (count($this->uploadsInfo) == 0) {
150 foreach ($postParams as $k => $v) {
152 $command .= ' -d ' . escapeshellarg($k . '=' . $v);
154 foreach ($v as $ak => $av) {
155 $command .= ' -d ' . escapeshellarg(
156 $k . '[' . $ak . ']=' . $av
162 foreach ($postParams as $k => $v) {
163 $command .= ' -F ' . escapeshellarg($k . '=' . $v);
165 foreach ($this->uploadsInfo as $fieldName => $filename) {
166 if (!is_array($filename)) {
167 $command .= ' -F ' . escapeshellarg(
168 $fieldName . '=@' . $filename
171 foreach ($filename as $k => $realFilename) {
172 $command .= ' -F ' . escapeshellarg(
173 $fieldName . '[' . $k . ']=@' . $realFilename
180 if ($this->dedicatedBody) {
181 $command .= ' --data ' . escapeshellarg($this->req->getBody());
184 $command .= ' ' . escapeshellarg((string) $this->req->getUrl());
189 public function setSendAsJson($json)
191 $this->sendAsJson = $json;