Text

Created 2016-06-08 06:17:21 by ANONYM, 859 views

URL: https://shre.in/9aE (click to copy)

Share



<?php
namespace Tools;
/**
 * Class Client
 * @package Tools\KvakWin
 */
class ShreInApiClient
{
    /**
     * @var string
     */
    protected $api_key;

    protected $endpoint = "http://www.shre.in/api/v1/";

    public function __construct($api_key)
    {
        $this->api_key = $api_key;
    }

    public function shortUrl($url, $is_private = "false", $password = null)
    {
        $params = ["URL" => $url];
        if ($is_private == "true" || $is_private === true)
            $params["PRIVATE"] = "true";
        if (is_string($password) && strlen($password) > 0)
            $params["PASSWORD"] = $password;
        $json = $this->_makeRequest("short-url", $params);
        return json_decode($json,true);
    }

    public function lookup($id){
        $json = $this->_makeRequest("short-url/".$id,[]);
        return json_decode($json,true);
    }

    /**
     * @param $route
     * @param array $params
     * @return string
     */
    private function _makeRequest($route, $params = [])
    {
        $params = array_merge(["API_KEY" => $this->api_key], $params);
        $ch = curl_init($this->endpoint . $route);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $dataR = curl_exec($ch);
        curl_close($ch);
        return $dataR;
    }
}