kukuhprabowo/curl-abstract

此包最新版本(dev-master)没有可用的许可证信息。

用于PHP中curl操作的抽象类。

dev-master 2016-01-10 16:48 UTC

This package is not auto-updated.

Last update: 2024-09-18 17:53:02 UTC


README

此包是用于PHP中curl操作的抽象类。有时在我们的应用程序中,我们希望通过HTTP连接将我们的应用程序与多个第三方服务连接起来。此包可以帮助您更容易地创建HTTP连接,并提供了许多不同的配置选项。

如何使用此包

{
  "require" : {
    "kukuhprabowo/curl-abstract" : "dev-master"
  }
}

GCM HTTP连接的示例

<?php
    required_once("vendor/autoload.php");

    use Kukuhprabowo\AbstractCurl;

    /**
     * This class GCM is for setup configuration and method
     * on Google cloud messaging. Just extends AbstractCurl
     * in your new class. 
     */
    class GCM extends AbstractCurl {
        public function __construct()
        {
            /**
             * Setting naming of your apps 
             * @type {String}
             */
            $this->apps       = 'Google Cloud Messaging Application';
            /**
             * set configuration curl options first
             * @type {Array}
             */
            $curl_set_options = [
                CURLOPT_CONNECTTIMEOUT => 30,
                CURLOPT_TIMEOUT        => 20,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_HEADER         => true,
                CURLOPT_FAILONERROR    => false,
            ];
            /**
             * set your configuration header
             * @type {Array}
             */
            $header = [
                'Authorization: 'key YOUR_API_KEY',
                'Content-Type: application/json',
            ];
            /**
             * set url endPoint for http connection
             * @type {String}
             */
            $this->endPoint = 'https://gcm-http.googleapis.com/gcm';
            /**
             * calling construct parent with curloptions and header, to make 
             * things goes right.
             */
            parent::__construct($curl_set_options, $header);
        }

        /**
         * create method on http connection with a function
         * @param  {Array}  $params Data that you want to send, it must be 
         *                          an array
         * @return ObjectClass return of this result will object class 
         *                     CurlResponse
         */
        public function send($params = [])
        {
            $this->method     = 'POST';
            $this->json       = true;
            $this->http_query = false;
            $service          = '/send;'

            $this->setup($service, $params);

            $data_response = $this->_send();

            return $data_response;
        }
    }

要查看完整的教程,请访问 此链接