9 protected $uploadsInfo = [];
11 public function __construct($host, $cfg)
14 $this->req = new MyHttpRequest2($host->endpoints->micropub, 'POST');
15 $this->req->setHeader('User-Agent: shpub');
16 if (version_compare(PHP_VERSION, '5.6.0', '<')) {
17 //correct ssl validation on php 5.5 is a pain, so disable
18 $this->req->setConfig('ssl_verify_host', false);
19 $this->req->setConfig('ssl_verify_peer', false);
21 $this->req->setHeader('Content-type', 'application/x-www-form-urlencoded');
22 $this->req->setHeader('authorization', 'Bearer ' . $host->token);
25 public function send($body = null)
28 $this->req->setBody($body);
30 if ($this->cfg->debug) {
33 $res = $this->req->send();
35 if (intval($res->getStatus() / 100) != 2) {
37 'Server returned an error status code ' . $res->getStatus()
39 Log::err($res->getBody());
46 * @param string $fieldName name of file-upload field
47 * @param string|resource|array $filename full name of local file,
48 * pointer to open file or an array of files
50 public function addUpload($fieldName, $filename)
52 $this->uploadsInfo[$fieldName] = $filename;
53 return $this->req->addUpload($fieldName, $filename);
56 protected function printCurl()
59 if ($this->req->getMethod() != 'GET') {
60 $command .= ' -X ' . $this->req->getMethod();
62 foreach ($this->req->getHeaders() as $key => $val) {
63 $caseKey = implode('-', array_map('ucfirst', explode('-', $key)));
64 $command .= ' -H ' . escapeshellarg($caseKey . ': ' . $val);
67 $postParams = $this->req->getPostParams();
69 if (count($this->uploadsInfo) == 0) {
70 foreach ($postParams as $k => $v) {
72 $command .= ' -d ' . escapeshellarg($k . '=' . $v);
74 foreach ($v as $ak => $av) {
75 $command .= ' -d ' . escapeshellarg(
76 $k . '[' . $ak . ']=' . $av
82 foreach ($postParams as $k => $v) {
83 $command .= ' -F ' . escapeshellarg($k . '=' . $v);
85 foreach ($this->uploadsInfo as $fieldName => $filename) {
86 if (!is_array($filename)) {
87 $command .= ' -F ' . escapeshellarg(
88 $fieldName . '=@' . $filename
91 foreach ($filename as $k => $realFilename) {
92 $command .= ' -F ' . escapeshellarg(
93 $fieldName . '[' . $k . ']=@' . $realFilename
100 $command .= ' ' . escapeshellarg((string) $this->req->getUrl());
102 echo $command . "\n";