CURL.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. namespace ZN\Services;
  3. class __USE_STATIC_ACCESS__CURL implements CURLInterface
  4. {
  5. //----------------------------------------------------------------------------------------------------
  6. //
  7. // Yazar : Ozan UYKUN <ozanbote@windowslive.com> | <ozanbote@gmail.com>
  8. // Site : www.zntr.net
  9. // Lisans : The MIT License
  10. // Telif Hakkı: Copyright (c) 2012-2016, zntr.net
  11. //
  12. //----------------------------------------------------------------------------------------------------
  13. /*
  14. * Tanıtıcı bilgisini tutar.
  15. *
  16. * @var resource
  17. */
  18. protected $init;
  19. /*
  20. * Seçenekler bilgisini tutar.
  21. *
  22. * @var array
  23. */
  24. protected $options = [];
  25. public function __construct()
  26. {
  27. if( ! function_exists('curl_exec') )
  28. {
  29. die(getErrorMessage('Error', 'undefinedFunction', 'curl_xxx'));
  30. }
  31. }
  32. use \CallUndefinedMethodTrait;
  33. /******************************************************************************************
  34. * INIT *
  35. *******************************************************************************************
  36. | Genel Kullanım: Bir CURL oturumunu ilklendirir. |
  37. @param string $url NULL
  38. @return resource
  39. | |
  40. ******************************************************************************************/
  41. public function init($url = CURLOPT_URL)
  42. {
  43. $this->init = curl_init($url);
  44. return $this;
  45. }
  46. /******************************************************************************************
  47. * EXEC *
  48. *******************************************************************************************
  49. | Genel Kullanım: Bir CURL oturumunu işleme sokar. |
  50. @param resource $ch;
  51. @return mixed
  52. | |
  53. ******************************************************************************************/
  54. public function exec()
  55. {
  56. if( ! is_resource($this->init) )
  57. {
  58. return false;
  59. }
  60. curl_setopt_array($this->init, $this->options);
  61. $this->options = [];
  62. if( is_resource($this->init) )
  63. {
  64. return curl_exec($this->init);
  65. }
  66. return false;
  67. }
  68. /******************************************************************************************
  69. * ESCAPE *
  70. *******************************************************************************************
  71. | Genel Kullanım: Verilen dizgenin URL'sini kodlar. |
  72. @param string $str
  73. @return string
  74. | |
  75. ******************************************************************************************/
  76. public function escape($str = '')
  77. {
  78. if( ! isPhpVersion('5.5.0') )
  79. {
  80. die(getErrorMessage('Error', 'invalidVersion', ['%' => 'curl_escape', '#' => '5.5.0']));
  81. }
  82. if( ! is_resource($this->init) )
  83. {
  84. return \Errors::set('Error', 'resourceParameter', '1.(ch)');
  85. }
  86. return curl_escape($this->init, $str);
  87. }
  88. /******************************************************************************************
  89. * UNESCAPE *
  90. *******************************************************************************************
  91. | Genel Kullanım: Şifrelenmiş URL verisini çözer. |
  92. @param string $str
  93. @return string
  94. | |
  95. ******************************************************************************************/
  96. public function unescape($str = '')
  97. {
  98. if( ! isPhpVersion('5.5.0') )
  99. {
  100. die(getErrorMessage('Error', 'invalidVersion', ['%' => 'curl_unescape', '#' => '5.5.0']));
  101. }
  102. if( ! is_resource($this->init) )
  103. {
  104. return \Errors::set('Error', 'resourceParameter', '1.(ch)');
  105. }
  106. return curl_unescape($this->init, $str);
  107. }
  108. /******************************************************************************************
  109. * INFO *
  110. *******************************************************************************************
  111. | Genel Kullanım: Belirtilen tanıtıcı ile ilgili son aktarım hakkında bilgi verir. |
  112. @param mixed $opt -> CURLINFO_ ön ekinin kullanılmasına gerek yoktur.
  113. CURLINFO_EFFECTIVE_URL yerine 'effective_url' gibi bir kullanım mümkündür.
  114. | |
  115. ******************************************************************************************/
  116. public function info($opt = 0)
  117. {
  118. if( ! is_resource($this->init) )
  119. {
  120. return \Errors::set('Error', 'resourceParameter', '1.(ch)');
  121. }
  122. return curl_getinfo($this->init, \Convert::toConstant($opt, 'CURLINFO_'));
  123. }
  124. /******************************************************************************************
  125. * ERROR *
  126. *******************************************************************************************
  127. | Genel Kullanım: CURL işlemleri esnasına oluşan hatalar hakkında bilgi almak için. |
  128. @param resource $ch
  129. | |
  130. ******************************************************************************************/
  131. public function error()
  132. {
  133. if( ! is_resource($this->init) )
  134. {
  135. return \Errors::set('Error', 'resourceParameter', '1.(ch)');
  136. }
  137. return curl_error($this->init);
  138. }
  139. /******************************************************************************************
  140. * ERRNO *
  141. *******************************************************************************************
  142. | Genel Kullanım: Belirtilen oturumdaki son hatanın kodunu döndürür. |
  143. @param resource $ch
  144. @return int
  145. | |
  146. ******************************************************************************************/
  147. public function errno()
  148. {
  149. if( ! is_resource($this->init) )
  150. {
  151. return \Errors::set('Error', 'resourceParameter', '1.(ch)');
  152. }
  153. return curl_errno($this->init);
  154. }
  155. /******************************************************************************************
  156. * PAUSE *
  157. *******************************************************************************************
  158. | Genel Kullanım: Bir bağlantıyı dururur. |
  159. @param int $bitmask 0
  160. @return int
  161. | |
  162. ******************************************************************************************/
  163. public function pause($bitmask = 0)
  164. {
  165. if( ! isPhpVersion('5.5.0') )
  166. {
  167. die(\Errors::message('Error', 'invalidVersion', ['%' => 'curl_pause', '#' => '5.5.0']));
  168. }
  169. if( ! empty($this->init) )
  170. {
  171. return curl_pause($this->init, $bitmask);
  172. }
  173. }
  174. /******************************************************************************************
  175. * RESET *
  176. *******************************************************************************************
  177. | Genel Kullanım: Bir CURL tanıcısını yeniden başlatır. |
  178. @param resource $ch
  179. @return void
  180. | |
  181. ******************************************************************************************/
  182. public function reset()
  183. {
  184. if( ! isPhpVersion('5.5.0') )
  185. {
  186. die(\Errors::message('Error', 'invalidVersion', ['%' => 'curl_reset', '#' => '5.5.0']));
  187. }
  188. if( ! empty($this->init) )
  189. {
  190. return curl_reset($this->init);
  191. }
  192. }
  193. /******************************************************************************************
  194. * OPTION *
  195. *******************************************************************************************
  196. | Genel Kullanım: Bir CURL aktarım seçeneği tanımlar. |
  197. @param mixed $options
  198. @param mixed $value
  199. @return bool
  200. | |
  201. ******************************************************************************************/
  202. public function option($options = 0, $value = '')
  203. {
  204. $this->options[\Convert::toConstant($options, 'CURLOPT_')] = $value;
  205. return $this;
  206. }
  207. /******************************************************************************************
  208. * CLOSE *
  209. *******************************************************************************************
  210. | Genel Kullanım: CURL oturumunu sonlandırır. |
  211. @param resource $ch
  212. @return void
  213. | |
  214. ******************************************************************************************/
  215. public function close()
  216. {
  217. $init = $this->init;
  218. if( is_resource($init) )
  219. {
  220. $this->init = NULL;
  221. return curl_close($init);
  222. }
  223. return false;
  224. }
  225. /******************************************************************************************
  226. * ERROR VAL *
  227. *******************************************************************************************
  228. | Genel Kullanım: Dönen hata numarasına göre hatanın içeriğini döndürür. |
  229. @param numeric $errno
  230. @return string
  231. | |
  232. ******************************************************************************************/
  233. public function errval($errno = 0)
  234. {
  235. if( ! isPhpVersion('5.5.0') )
  236. {
  237. die(\Errors::message('Error', 'invalidVersion', ['%' => 'curl_version', '#' => '5.5.0']));
  238. }
  239. if( ! is_numeric($errno) )
  240. {
  241. return \Errors::set('Error', 'numericParameter', '1.(errno)');
  242. }
  243. return curl_strerror($errno);
  244. }
  245. /******************************************************************************************
  246. * VERSION *
  247. *******************************************************************************************
  248. | Genel Kullanım: Geçerli CURL versiyonu hakkında bir dizi döndürür. |
  249. @param mixed $age now
  250. @return array
  251. | |
  252. ******************************************************************************************/
  253. public function version($data = NULL)
  254. {
  255. $version = curl_version();
  256. if( $data === NULL )
  257. {
  258. return $version;
  259. }
  260. else
  261. {
  262. if( isset($version[$data]) )
  263. {
  264. return $version[$data];
  265. }
  266. else
  267. {
  268. return false;
  269. }
  270. }
  271. }
  272. //----------------------------------------------------------------------------------------------------
  273. // __destruct()
  274. //----------------------------------------------------------------------------------------------------
  275. //
  276. // @param void
  277. //
  278. //----------------------------------------------------------------------------------------------------
  279. public function __destruct()
  280. {
  281. $this->close();
  282. }
  283. }