Overview

Namespaces

  • Kotchasan
    • Cache
    • Database
    • Http
    • Log
    • Orm
  • None
  • PHP
  • Psr
    • Cache
    • Http
      • Message
    • Log

Classes

  • ArrayIterator
  • DateInterval
  • DOMNode
  • Kotchasan
  • Kotchasan\Accordion
  • Kotchasan\ApiController
  • Kotchasan\ArrayTool
  • Kotchasan\Cache\ApcCache
  • Kotchasan\Cache\Cache
  • Kotchasan\Cache\CacheItem
  • Kotchasan\Cache\FileCache
  • Kotchasan\CKEditor
  • Kotchasan\Collection
  • Kotchasan\Config
  • Kotchasan\Controller
  • Kotchasan\Country
  • Kotchasan\Csv
  • Kotchasan\Curl
  • Kotchasan\Currency
  • Kotchasan\Database
  • Kotchasan\Database\Db
  • Kotchasan\Database\DbCache
  • Kotchasan\Database\Driver
  • Kotchasan\Database\PdoMysqlDriver
  • Kotchasan\Database\Query
  • Kotchasan\Database\QueryBuilder
  • Kotchasan\Database\Schema
  • Kotchasan\Database\Sql
  • Kotchasan\DataTable
  • Kotchasan\Date
  • Kotchasan\DOMNode
  • Kotchasan\DOMParser
  • Kotchasan\Email
  • Kotchasan\File
  • Kotchasan\Files
  • Kotchasan\Form
  • Kotchasan\Grid
  • Kotchasan\Html
  • Kotchasan\Htmldoc
  • Kotchasan\HtmlTable
  • Kotchasan\Http\AbstractMessage
  • Kotchasan\Http\AbstractRequest
  • Kotchasan\Http\Message
  • Kotchasan\Http\NotFound
  • Kotchasan\Http\Request
  • Kotchasan\Http\Response
  • Kotchasan\Http\Stream
  • Kotchasan\Http\UploadedFile
  • Kotchasan\Http\Uri
  • Kotchasan\Image
  • Kotchasan\InputItem
  • Kotchasan\Inputs
  • Kotchasan\KBase
  • Kotchasan\Language
  • Kotchasan\ListItem
  • Kotchasan\Log\AbstractLogger
  • Kotchasan\Log\Logger
  • Kotchasan\Login
  • Kotchasan\Menu
  • Kotchasan\Mime
  • Kotchasan\Model
  • Kotchasan\Number
  • Kotchasan\ObjectTool
  • Kotchasan\Orm\Field
  • Kotchasan\Orm\Recordset
  • Kotchasan\Password
  • Kotchasan\Pdf
  • Kotchasan\Province
  • Kotchasan\Router
  • Kotchasan\Session
  • Kotchasan\Singleton
  • Kotchasan\Tab
  • Kotchasan\TableRow
  • Kotchasan\Template
  • Kotchasan\Text
  • Kotchasan\Validator
  • Kotchasan\View
  • PHPMailer
  • Psr\Log\AbstractLogger
  • Psr\Log\LogLevel
  • Psr\Log\NullLogger
  • SMTP

Interfaces

  • ArrayAccess
  • Countable
  • DateTimeInterface
  • Iterator
  • IteratorAggregate
  • Psr\Cache\CacheItemInterface
  • Psr\Cache\CacheItemPoolInterface
  • Psr\Http\Message\MessageInterface
  • Psr\Http\Message\RequestInterface
  • Psr\Http\Message\ResponseInterface
  • Psr\Http\Message\ServerRequestInterface
  • Psr\Http\Message\StreamInterface
  • Psr\Http\Message\UploadedFileInterface
  • Psr\Http\Message\UriInterface
  • Psr\Log\LoggerAwareInterface
  • Psr\Log\LoggerInterface
  • SeekableIterator
  • Serializable
  • Traversable

Traits

  • Psr\Log\LoggerTrait

Exceptions

  • Exception
  • InvalidArgumentException
  • Kotchasan\ApiException
  • Kotchasan\Cache\Exception
  • Kotchasan\Database\Exception
  • Kotchasan\InputItemException
  • LogicException
  • phpmailerException
  • RuntimeException

Functions

  • createClass
  • debug
  • getClassPath
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * @filesource Kotchasan/Curl.php
  4:  *
  5:  * @copyright 2016 Goragod.com
  6:  * @license http://www.kotchasan.com/license/
  7:  *
  8:  * @see http://www.kotchasan.com/
  9:  */
 10: 
 11: namespace Kotchasan;
 12: 
 13: /**
 14:  * Curl Class.
 15:  *
 16:  * @author Goragod Wiriya <admin@goragod.com>
 17:  *
 18:  * @since 1.0
 19:  */
 20: class Curl
 21: {
 22:     /**
 23:      * ตัวแปรสำหรับเก็บ Error ที่มาจาก cURL
 24:      * 0 ไม่มี error (ค่าเริ่มต้น)
 25:      * มากกว่า 0 Error No. ของ cURL.
 26:      *
 27:      * @var int
 28:      */
 29:     protected $error = 0;
 30:     /**
 31:      * ข้อความ Error จาก cURL หากมีข้อผิดพลาดในการส่ง.
 32:      *
 33:      * @var string
 34:      */
 35:     protected $errorMessage = '';
 36:     /**
 37:      * HTTP headers.
 38:      *
 39:      * @var array
 40:      */
 41:     protected $headers = array();
 42:     /**
 43:      * พารามิเตอร์ CURLOPT.
 44:      *
 45:      * @var array
 46:      */
 47:     protected $options = array();
 48: 
 49:     /**
 50:      * Construct
 51:      *
 52:      * @throws \Exception ถ้าไม่รองรับ cURL
 53:      */
 54:     public function __construct()
 55:     {
 56:         if (!extension_loaded('curl')) {
 57:             throw new \Exception('cURL library is not loaded');
 58:         }
 59:         // default parameter
 60:         $this->headers = array(
 61:             'Connection' => 'keep-alive',
 62:             'Keep-Alive' => '300',
 63:             'Accept-Charset' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
 64:             'Accept-Language' => 'en-us,en;q=0.5',
 65:         );
 66:         $this->options = array(
 67:             CURLOPT_TIMEOUT => 30,
 68:             CURLOPT_RETURNTRANSFER => true,
 69:             CURLOPT_USERAGENT => 'Googlebot/2.1 (+http://www.google.com/bot.html)',
 70:             CURLOPT_SSL_VERIFYHOST => false,
 71:             CURLOPT_SSL_VERIFYPEER => false,
 72:         );
 73:     }
 74: 
 75:     /**
 76:      * DELETE.
 77:      *
 78:      * @param string $url
 79:      * @param mix    $params
 80:      *
 81:      * @return string
 82:      */
 83:     public function delete($url, $params)
 84:     {
 85:         $this->options[CURLOPT_CUSTOMREQUEST] = 'DELETE';
 86:         if (is_array($params)) {
 87:             $this->options[CURLOPT_POSTFIELDS] = http_build_query($params, '', '&');
 88:         } else {
 89:             $this->options[CURLOPT_POSTFIELDS] = $params;
 90:         }
 91: 
 92:         return $this->execute($url);
 93:     }
 94: 
 95:     /**
 96:      * คืนค่า error no จากการ cURL
 97:      * 0 หมายถึงไม่มี error.
 98:      *
 99:      * @return int
100:      */
101:     public function error()
102:     {
103:         return $this->error;
104:     }
105: 
106:     /**
107:      * คืนค่าข้อความ Error จาก cURL หากมีข้อผิดพลาดในการส่ง.
108:      *
109:      * @return string
110:      */
111:     public function errorMessage()
112:     {
113:         return $this->errorMessage;
114:     }
115: 
116:     /**
117:      * GET.
118:      *
119:      * @param string $url
120:      * @param mix    $params
121:      *
122:      * @return string
123:      */
124:     public function get($url, $params = array())
125:     {
126:         $this->options[CURLOPT_CUSTOMREQUEST] = 'GET';
127:         $this->options[CURLOPT_HTTPGET] = true;
128:         if (is_array($params)) {
129:             $url .= (strpos($url, '?') === false ? '?' : '&').http_build_query($params, '', '&');
130:         } else {
131:             $this->options[CURLOPT_POSTFIELDS] = $params;
132:         }
133: 
134:         return $this->execute($url);
135:     }
136: 
137:     /**
138:      * HEAD.
139:      *
140:      * @param string $url
141:      * @param mix    $params
142:      *
143:      * @return string
144:      */
145:     public function head($url, $params = array())
146:     {
147:         $this->options[CURLOPT_CUSTOMREQUEST] = 'HEAD';
148:         $this->options[CURLOPT_NOBODY] = true;
149:         if (is_array($params)) {
150:             $this->options[CURLOPT_POSTFIELDS] = http_build_query($params, '', '&');
151:         } else {
152:             $this->options[CURLOPT_POSTFIELDS] = $params;
153:         }
154: 
155:         return $this->execute($url);
156:     }
157: 
158:     /**
159:      * Login สำหรับการส่งแบบ HTTP.
160:      *
161:      * @param string $username
162:      * @param string $password
163:      * @param string $type     any (default), digest, basic, digest_ie, negotiate, ntlm, ntlm_wb, anysafe, only
164:      *
165:      * @return $this
166:      */
167:     public function httpauth($username = '', $password = '', $type = 'any')
168:     {
169:         $this->options[CURLOPT_HTTPAUTH] = constant('CURLAUTH_'.strtoupper($type));
170:         $this->options[CURLOPT_USERPWD] = $username.':'.$password;
171: 
172:         return $this;
173:     }
174: 
175:     /**
176:      * ใช้งาน PROXY.
177:      *
178:      * @param string $url
179:      * @param int    $port
180:      * @param string $username
181:      * @param string $password
182:      *
183:      * @return $this
184:      */
185:     public function httpproxy($url = '', $port = 80, $username = null, $password = null)
186:     {
187:         $this->options[CURLOPT_HTTPPROXYTUNNEL] = true;
188:         $this->options[CURLOPT_PROXY] = $url.':'.$port;
189:         if ($username !== null && $password !== null) {
190:             $this->options[CURLOPT_PROXYUSERPWD] = $username.':'.$password;
191:         }
192: 
193:         return $this;
194:     }
195: 
196:     /**
197:      * POST.
198:      *
199:      * @param string $url
200:      * @param mix    $params
201:      *
202:      * @return string
203:      */
204:     public function post($url, $params = array())
205:     {
206:         $this->options[CURLOPT_CUSTOMREQUEST] = 'POST';
207:         $this->options[CURLOPT_POST] = true;
208:         if (is_array($params)) {
209:             $this->options[CURLOPT_POSTFIELDS] = http_build_query($params, '', '&');
210:         } else {
211:             $this->options[CURLOPT_POSTFIELDS] = $params;
212:         }
213: 
214:         return $this->execute($url);
215:     }
216: 
217:     /**
218:      * PUT.
219:      *
220:      * @param string $url
221:      * @param mix    $params
222:      *
223:      * @return string
224:      */
225:     public function put($url, $params = array())
226:     {
227:         $this->options[CURLOPT_CUSTOMREQUEST] = 'PUT';
228:         if (is_array($params)) {
229:             $this->options[CURLOPT_POSTFIELDS] = http_build_query($params, '', '&');
230:         } else {
231:             $this->options[CURLOPT_POSTFIELDS] = $params;
232:         }
233: 
234:         return $this->execute($url);
235:     }
236: 
237:     /**
238:      * กำหนด referer.
239:      *
240:      * @param string $referrer
241:      *
242:      * @return $this
243:      */
244:     public function referer($referrer)
245:     {
246:         $this->options[CURLOPT_REFERER] = $referrer;
247: 
248:         return $this;
249:     }
250: 
251:     /**
252:      * กำหนดค่า cookie file.
253:      *
254:      * @param string $cookiePath
255:      *
256:      * @return $this
257:      */
258:     public function setCookie($cookiePath)
259:     {
260:         $this->options[CURLOPT_COOKIEFILE] = $cookiePath;
261:         $this->options[CURLOPT_COOKIEJAR] = $cookiePath;
262: 
263:         return $this;
264:     }
265: 
266:     /**
267:      * กำหนด Header.
268:      *
269:      * @param array $headers
270:      *
271:      * @return $this
272:      */
273:     public function setHeaders($headers)
274:     {
275:         foreach ($headers as $key => $value) {
276:             $this->headers[$key] = $value;
277:         }
278: 
279:         return $this;
280:     }
281: 
282:     /**
283:      * กำหนด Options.
284:      *
285:      * @param array $options
286:      *
287:      * @return $this
288:      */
289:     public function setOptions($options)
290:     {
291:         foreach ($options as $key => $value) {
292:             $this->options[$key] = $value;
293:         }
294: 
295:         return $this;
296:     }
297: 
298:     /**
299:      * ประมวลผล cURL.
300:      *
301:      * @param string $url
302:      *
303:      * @return string
304:      */
305:     protected function execute($url)
306:     {
307:         $ch = curl_init();
308:         curl_setopt($ch, CURLOPT_URL, $url);
309:         if (!empty($this->headers)) {
310:             $headers = array();
311:             foreach ($this->headers as $key => $value) {
312:                 $headers[] = $key.': '.$value;
313:             }
314:             curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
315:         }
316:         foreach ($this->options as $key => $value) {
317:             curl_setopt($ch, $key, $value);
318:         }
319:         $response = curl_exec($ch);
320:         if (curl_error($ch)) {
321:             $this->error = curl_errno($ch);
322:             $this->errorMessage = curl_error($ch);
323:         }
324:         curl_close($ch);
325: 
326:         return $response;
327:     }
328: }
329: 
Kotchasan API documentation generated by ApiGen