reply support
[shpub.git] / src / shpub / Request.php
diff --git a/src/shpub/Request.php b/src/shpub/Request.php
new file mode 100644 (file)
index 0000000..2896c81
--- /dev/null
@@ -0,0 +1,33 @@
+<?php
+namespace shpub;
+
+class Request
+{
+    public $req;
+
+    public function __construct($host)
+    {
+        $this->req = new \HTTP_Request2($host->endpoints->micropub, 'POST');
+        if (version_compare(PHP_VERSION, '5.6.0', '<')) {
+            //correct ssl validation on php 5.5 is a pain, so disable
+            $this->req->setConfig('ssl_verify_host', false);
+            $this->req->setConfig('ssl_verify_peer', false);
+        }
+        $this->req->setHeader('Content-type', 'application/x-www-form-urlencoded');
+        $this->req->setHeader('Authorization', 'Bearer ' . $host->token);
+    }
+
+    public function send($body)
+    {
+        $this->req->setBody($body);
+        $res = $this->req->send();
+        if (intval($res->getStatus() / 100) != 2) {
+            Log::err(
+                'Server returned an error status code ' . $res->getStatus()
+            );
+            Log::err($res->getBody());
+            exit(11);
+        }
+        return $res;
+    }
+}
\ No newline at end of file