How can i pass filter for a API rest like WHERE field = 'value'?

时间:2016-08-31 18:06:40

标签: php rest api

I want to pass filter to it for get, for example, one single row based on this filters but i don't know how to do that.

<?php
namespace app\Model
{
    class RestFull
    {
        private $url = 'http://myurl.azurewebsites.net/tables/';
        private $curl;
        private $response;
        private $table;

        public function __construct(){}

        /**
         * Start the API RestFull
         * ---- ~ ----
         */
        public function apiRest($table, $id = null)
        {
            $this->table = $table;
            $this->setUrl($id);
            $this->initCurl();
            $this->setHeader();
        }   

        /**
         * Get data from API
         * Method GET
         */
        public function restGet()
        {
            $this->response = curl_exec($this->curl);
            $this->getErrors();
            curl_close($this->curl);
        }

        /**
         * Post data for API
         * Method POST
         * @param array $data
         */
        public function restPost(array $data)
        {
            $data = json_encode($data);
            curl_setopt($this->curl, CURLOPT_POST, true);
            curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data);
            $this->response = curl_exec($this->curl);
            $this->getErrors();
            curl_close($this->curl);
        }

        /**
         *
         */
        public function restPut()
        {

        }       

        /**
         *
         */
        public function restDelete()
        {

        }

        /**
         * Return the response
         * @return json
         */
        public function setResponse()
        {
            return $this->response;
        }

        /**
         * Generate the url value
         */
        private function setUrl($id = null)
        {
            $id = (!empty($id) ? "/{$id}" : null);
            $this->url = $this->url . $this->table . $id;
        }

        /**
         * Init Curl
         * ---- 1 ----
         */
        private function initCurl()
        {
            $this->curl = curl_init($this->url);
        }

        /**
         * Set headers
         * ---- 2 ----
         */
        private function setHeader()
        {
            curl_setopt($this->curl, CURLOPT_HTTPHEADER, ['zumo-api-version: 2.0.0', 'Content-Type: application/json', 'Accept: application/json']);
            curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
        }

        /**
         * Errors
         * @return string
         */
        private function getErrors()
        {
            if($this->curl === false):
                curl_close($this->curl);
                return 'An error occured during curl exec. Additioanl info: ' . var_export(curl_getinfo($this->curl));
            endif;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

这是REST服务器还是客户端? 如果是客户端,则取决于服务器支持的内容。可以是帖子值:

$data = array(
     "key" => "value"
);

curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data);

或者可以将字符串附加到URL:

?api_key=civicrm123

如果是服务器,则需要将查询绑定到模型,以便在查询运行以检索数据时过滤结果。