X-Git-Url: https://git.cweiske.de/shpub.git/blobdiff_plain/3c23b2cde4f8d6de57d5e1cc86828d6d200da65d..059c6ab77b8e63210b9afc266d62c9f543d4d7ae:/src/shpub/Command/Like.php?ds=sidebyside diff --git a/src/shpub/Command/Like.php b/src/shpub/Command/Like.php index 70e836f..ad19b8f 100644 --- a/src/shpub/Command/Like.php +++ b/src/shpub/Command/Like.php @@ -1,47 +1,44 @@ host = $host; + $cmd = $optParser->addCommand('like'); + static::optsGeneric($cmd); + $cmd->addArgument( + 'url', + [ + 'optional' => false, + 'description' => 'URL that is liked', + ] + ); } - public function run($url) + public function run(\Console_CommandLine_Result $cmdRes) { - $url = Validator::url($url, 'url'); + $url = Validator::url($cmdRes->args['url'], 'url'); if ($url === false) { exit(10); } - $body = http_build_query( - [ - 'h' => 'entry', - 'like-of' => $url, - ] - ); + $req = new Request($this->cfg->host, $this->cfg); + $req->setType('entry'); + $req->addProperty('like-of', $url); - $req = new \HTTP_Request2($this->host->endpoints->micropub, 'POST'); - $req->setHeader('Content-type', 'application/x-www-form-urlencoded'); - $req->setHeader('Authorization', 'Bearer ' . $this->host->token); - $req->setBody($body); + $this->handleGenericOptions($cmdRes, $req); $res = $req->send(); - if (intval($res->getStatus() / 100) != 2) { - Log::err( - 'Server returned an error status code ' . $res->getStatus() - ); + + $postUrl = $res->getHeader('Location'); + if ($postUrl === null) { + Log::err('Error: Server sent no "Location" header and said:'); Log::err($res->getBody()); - exit(11); + exit(20); + } else { + Log::info('Like created at server'); + Log::msg($postUrl); } - $postUrl = $res->getHeader('Location'); - echo "Like created at server\n"; - echo $postUrl . "\n"; } } ?>