70e836f50b1f0ea95c06ab76132ee76b4fc08f57
[shpub.git] / src / shpub / Command / Like.php
1 <?php
2 namespace shpub;
3
4 class Command_Like
5 {
6     /**
7      * @var Config_Host
8      */
9     protected $host;
10
11     public function __construct($host)
12     {
13         $this->host = $host;
14     }
15
16     public function run($url)
17     {
18         $url = Validator::url($url, 'url');
19         if ($url === false) {
20             exit(10);
21         }
22
23         $body = http_build_query(
24             [
25                 'h'       => 'entry',
26                 'like-of' => $url,
27             ]
28         );
29
30         $req = new \HTTP_Request2($this->host->endpoints->micropub, 'POST');
31         $req->setHeader('Content-type', 'application/x-www-form-urlencoded');
32         $req->setHeader('Authorization', 'Bearer ' . $this->host->token);
33         $req->setBody($body);
34         $res = $req->send();
35         if (intval($res->getStatus() / 100) != 2) {
36             Log::err(
37                 'Server returned an error status code ' . $res->getStatus()
38             );
39             Log::err($res->getBody());
40             exit(11);
41         }
42         $postUrl = $res->getHeader('Location');
43         echo "Like created at server\n";
44         echo $postUrl . "\n";
45     }
46 }
47 ?>