9 protected $uploadsInfo = [];
10 protected $dedicatedBody = false;
12 public function __construct($host, $cfg)
15 $this->req = new MyHttpRequest2($host->endpoints->micropub, 'POST');
16 $this->req->setHeader('User-Agent: shpub');
17 if (version_compare(PHP_VERSION, '5.6.0', '<')) {
18 //correct ssl validation on php 5.5 is a pain, so disable
19 $this->req->setConfig('ssl_verify_host', false);
20 $this->req->setConfig('ssl_verify_peer', false);
22 $this->req->setHeader('Content-type', 'application/x-www-form-urlencoded');
23 $this->req->setHeader('authorization', 'Bearer ' . $host->token);
26 public function send($body = null)
29 $this->dedicatedBody = true;
30 $this->req->setBody($body);
32 if ($this->cfg->debug) {
35 $res = $this->req->send();
37 if (intval($res->getStatus() / 100) != 2) {
39 'Server returned an error status code ' . $res->getStatus()
41 Log::err($res->getBody());
48 * @param string $fieldName name of file-upload field
49 * @param string|resource|array $filename full name of local file,
50 * pointer to open file or an array of files
52 public function addUpload($fieldName, $filename)
54 $this->uploadsInfo[$fieldName] = $filename;
55 return $this->req->addUpload($fieldName, $filename);
59 * Add one or multiple POST parameters.
60 * Automatically adds them as array or as string.
62 * @param string $key Parameter name
63 * @param string|array $values One or multiple values
65 public function addPostParameter($key, $values)
67 if (count($values) == 1) {
68 $values = reset($values);
70 $this->req->addPostParameter($key, $values);
73 protected function printCurl()
76 if ($this->req->getMethod() != 'GET') {
77 $command .= ' -X ' . $this->req->getMethod();
79 foreach ($this->req->getHeaders() as $key => $val) {
80 $caseKey = implode('-', array_map('ucfirst', explode('-', $key)));
81 $command .= ' -H ' . escapeshellarg($caseKey . ': ' . $val);
84 $postParams = $this->req->getPostParams();
86 if (count($this->uploadsInfo) == 0) {
87 foreach ($postParams as $k => $v) {
89 $command .= ' -d ' . escapeshellarg($k . '=' . $v);
91 foreach ($v as $ak => $av) {
92 $command .= ' -d ' . escapeshellarg(
93 $k . '[' . $ak . ']=' . $av
99 foreach ($postParams as $k => $v) {
100 $command .= ' -F ' . escapeshellarg($k . '=' . $v);
102 foreach ($this->uploadsInfo as $fieldName => $filename) {
103 if (!is_array($filename)) {
104 $command .= ' -F ' . escapeshellarg(
105 $fieldName . '=@' . $filename
108 foreach ($filename as $k => $realFilename) {
109 $command .= ' -F ' . escapeshellarg(
110 $fieldName . '[' . $k . ']=@' . $realFilename
117 if ($this->dedicatedBody) {
118 $command .= ' --data ' . escapeshellarg($this->req->getBody());
121 $command .= ' ' . escapeshellarg((string) $this->req->getUrl());