api.omaeurl.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. <?php
  2. /**
  3. * Basic remote URLs interaction class
  4. */
  5. class OmaeUrl {
  6. /**
  7. * Contains current instance URL
  8. *
  9. * @var string
  10. */
  11. protected $url = '';
  12. /**
  13. * Contains default connection timeout in seconds
  14. *
  15. * @var int
  16. */
  17. protected $timeout = 2;
  18. /**
  19. * Last curl error description
  20. *
  21. * @var string
  22. */
  23. protected $errorMessage = '';
  24. /**
  25. * Last curl error code
  26. *
  27. * @var int
  28. */
  29. protected $errorCode = 0;
  30. /**
  31. * Contains last request http code
  32. *
  33. * @var int
  34. */
  35. protected $httpCode = 0;
  36. /**
  37. * Contains last request curl info array or false on error
  38. *
  39. * @var array/bool
  40. */
  41. protected $lastRequestInfo = array();
  42. /**
  43. * Is error happens flag
  44. *
  45. * @var bool
  46. */
  47. protected $error = false;
  48. /**
  49. * Contains post data array that will be pushed to remote URL
  50. *
  51. * @var array
  52. */
  53. protected $postData = array();
  54. /**
  55. * Contains RAW-post data just as text
  56. *
  57. * @var string
  58. */
  59. protected $rawPostData = '';
  60. /**
  61. * Contains get data that will be mixed into URL on requests
  62. *
  63. * @var array
  64. */
  65. protected $getData = array();
  66. /**
  67. * Contains cookie data as cookiename=>data
  68. *
  69. * @var array
  70. */
  71. protected $cookieData = array();
  72. /**
  73. * Contains current instance headers as headername=>value
  74. *
  75. * @var array
  76. */
  77. protected $headersData = array();
  78. /**
  79. * Contains default user agent
  80. *
  81. * @var string
  82. */
  83. protected $userAgent = '';
  84. /**
  85. * Contains current instance curl options array as option=>value
  86. *
  87. * @var array
  88. */
  89. protected $curlOpts = array();
  90. /**
  91. * Get headers flag
  92. *
  93. * @var bool
  94. */
  95. protected $headersFlag = false;
  96. /**
  97. * Request referrer
  98. *
  99. * @var string
  100. */
  101. protected $referrer = '';
  102. /**
  103. * Enable CURL verbose logging
  104. *
  105. * @var bool
  106. */
  107. protected $verboseLogON = false;
  108. /**
  109. * Placeholder for CURL verbose logging stream
  110. *
  111. * @var string
  112. */
  113. protected $verboseLogStream = '';
  114. /**
  115. * Placeholder for CURL verbose log file
  116. *
  117. * @var string
  118. */
  119. protected $verboseLogFilePath = '';
  120. /**
  121. * Contains CURL version as a 3-digits integer
  122. *
  123. * @var int
  124. */
  125. protected $curlVersion = 0;
  126. const DEFAULT_VERBOSE_LOG_PATH = 'exports/OMAE_VERBOSE_LOG';
  127. /**
  128. * Creates new omae wa mou shindeiru instance
  129. *
  130. * @param string $url
  131. *
  132. * @throws Exception
  133. */
  134. public function __construct($url = '') {
  135. if ($this->checkModCurl()) {
  136. $this->setUrl($url);
  137. $this->loadOpts();
  138. $this->curlVersion = curl_version();
  139. $this->curlVersion = intval(str_replace('.', '', substr($this->curlVersion['version'], 0, 5)));
  140. } else {
  141. throw new Exception('SHINDEIRU_NO_CURL_EXTENSION');
  142. }
  143. }
  144. /**
  145. * Sets instance URL
  146. *
  147. * @param string $url
  148. *
  149. * @return void
  150. */
  151. protected function setUrl($url = '') {
  152. $this->url = $url;
  153. }
  154. /**
  155. * Checks is curl PHP extension loaded?
  156. *
  157. * @return bool
  158. */
  159. protected function checkModCurl() {
  160. $result = true;
  161. if (!extension_loaded('curl')) {
  162. $result = false;
  163. }
  164. return($result);
  165. }
  166. /**
  167. * Sets return headers flag
  168. *
  169. * @param bool $state
  170. *
  171. * @return void
  172. */
  173. public function setHeadersReturn($state) {
  174. $this->headersFlag = $state;
  175. $this->setOpt(CURLOPT_HEADER, $this->headersFlag);
  176. }
  177. /**
  178. * Sets instance referrer URL
  179. *
  180. * @param string $url
  181. *
  182. * @return void
  183. */
  184. public function setReferrer($url) {
  185. $this->referrer = $url;
  186. $this->setOpt(CURLOPT_REFERER, $this->referrer);
  187. }
  188. /**
  189. * Sets default instance curl options
  190. *
  191. * @return void
  192. */
  193. protected function loadOpts() {
  194. $this->setOpt(CURLOPT_CONNECTTIMEOUT, $this->timeout);
  195. $this->setOpt(CURLOPT_HEADER, false);
  196. $this->setOpt(CURLOPT_FOLLOWLOCATION, true);
  197. $this->setOpt(CURLOPT_MAXREDIRS, 10);
  198. $this->setOpt(CURLOPT_SSL_VERIFYHOST, false);
  199. $this->setOpt(CURLOPT_SSL_VERIFYPEER, false);
  200. $this->setOpt(CURLOPT_RETURNTRANSFER, true);
  201. }
  202. /**
  203. * Puts some data into protected postData property for further usage
  204. *
  205. * @param string $field record field name to push data
  206. * @param string $value field content to push
  207. *
  208. * @return void
  209. */
  210. public function dataPost($field = '', $value = '') {
  211. if (!empty($field)) {
  212. $this->postData[$field] = $value;
  213. } else {
  214. $this->flushPostData();
  215. }
  216. }
  217. /**
  218. * Puts some data into protected rawPostData property for further usage in POST requests
  219. *
  220. * @param string $body content body to push
  221. *
  222. * @return void
  223. */
  224. public function dataPostRaw($body = '') {
  225. if (!empty($body)) {
  226. $this->rawPostData = $body;
  227. } else {
  228. $this->flushRawPostData();
  229. }
  230. }
  231. /**
  232. * Puts some data into protected getData property for further usage
  233. *
  234. * @param string $field record field name to push data
  235. * @param string $value field content to push
  236. *
  237. * @return void
  238. */
  239. public function dataGet($field = '', $value = '') {
  240. if (!empty($field)) {
  241. $this->getData[$field] = $value;
  242. } else {
  243. $this->flushGetData();
  244. }
  245. }
  246. /**
  247. * Puts some data into protected cookieData property for further usage
  248. *
  249. * @param string $name record field name to push data
  250. * @param string $value field content to push
  251. *
  252. * @return void
  253. */
  254. public function dataCookie($name = '', $value = '') {
  255. if (!empty($name)) {
  256. $this->cookieData[$name] = $value;
  257. } else {
  258. $this->flushCookieData();
  259. }
  260. }
  261. /**
  262. * Puts some data into protected headersData property for further usage
  263. *
  264. * @param string $name record field name to push data
  265. * @param string $value field content to push
  266. *
  267. * @return void
  268. */
  269. public function dataHeader($name = '', $value = '') {
  270. if (!empty($name)) {
  271. $this->headersData[$name] = $value;
  272. } else {
  273. $this->flushHeadersData();
  274. }
  275. }
  276. /**
  277. * Flushes current instance postData set
  278. *
  279. * @return void
  280. */
  281. protected function flushPostData() {
  282. $this->postData = array();
  283. }
  284. /**
  285. * Flushes current instance rawPostData content
  286. *
  287. * @return void
  288. */
  289. protected function flushRawPostData() {
  290. $this->rawPostData = '';
  291. }
  292. /**
  293. * Flushes current instance getData set
  294. *
  295. * @return void
  296. */
  297. protected function flushGetData() {
  298. $this->getData = array();
  299. }
  300. /**
  301. * Flushes current instance cookieData set
  302. *
  303. * @return void
  304. */
  305. protected function flushCookieData() {
  306. $this->cookieData = array();
  307. }
  308. /**
  309. * Flushes current instance headersData set
  310. *
  311. * @return void
  312. */
  313. protected function flushHeadersData() {
  314. $this->headersData = array();
  315. }
  316. /**
  317. * Sets curl resource option for further usage
  318. *
  319. * @param string $option
  320. * @param mixed $value
  321. *
  322. * @return void
  323. */
  324. public function setOpt($option, $value) {
  325. $this->curlOpts[$option] = $value;
  326. }
  327. /**
  328. * Returns some data from remote source URL
  329. *
  330. * @return string
  331. *
  332. * @throws Exception
  333. */
  334. public function response($url = '') {
  335. $result = '';
  336. if (!empty($url)) {
  337. $this->setUrl($url);
  338. }
  339. if (!empty($this->url)) {
  340. if ($this->verboseLogON) {
  341. $this->verboseLogStream = fopen('php://temp', 'w+');
  342. $this->setOpt(CURLOPT_VERBOSE, true);
  343. $this->setOpt(CURLOPT_STDERR, $this->verboseLogStream);
  344. if ($this->curlVersion >= 719) {
  345. $this->setOpt(CURLOPT_CERTINFO, true);
  346. }
  347. }
  348. $remoteUrl = $this->url;
  349. //appending GET vars to URL
  350. if (!empty($this->getData)) {
  351. if (strpos($this->url, '?') === false) {
  352. $remoteUrl .= '?';
  353. }
  354. foreach ($this->getData as $getKey => $getValue) {
  355. $remoteUrl .= '&' . $getKey . '=' . $getValue . '&';
  356. }
  357. }
  358. //appending RAW POST request body as is
  359. if (!empty($this->rawPostData)) {
  360. $this->setOpt(CURLOPT_POSTFIELDS, $this->rawPostData);
  361. }
  362. //appending POST vars into options
  363. if (!empty($this->postData)) {
  364. $postFields = '';
  365. foreach ($this->postData as $postKey => $postValue) {
  366. $postFields .= $postKey . '=' . $postValue . '&';
  367. }
  368. $this->setOpt(CURLOPT_POSTFIELDS, $postFields);
  369. }
  370. //appending cookie data into options
  371. if (!empty($this->cookieData)) {
  372. $this->setOpt(CURLOPT_COOKIE, implode('; ', array_map(function ($k, $v) {
  373. return $k . '=' . $v;
  374. }, array_keys($this->cookieData), array_values($this->cookieData))));
  375. }
  376. //and some custom headers
  377. if (!empty($this->headersData)) {
  378. $headersTmp = array();
  379. foreach ($this->headersData as $headerKey => $headerValue) {
  380. $headersTmp[] = $headerKey . ':' . $headerValue;
  381. }
  382. $this->setOpt(CURLOPT_HTTPHEADER, $headersTmp);
  383. }
  384. /**
  385. * Ora ora ora ora ora ora
  386. */
  387. $ch = curl_init($remoteUrl);
  388. //setting resource options before exec
  389. if (!empty($this->curlOpts)) {
  390. curl_setopt_array($ch, $this->curlOpts);
  391. }
  392. //executing request
  393. $result .= curl_exec($ch);
  394. $this->errorCode = curl_errno($ch);
  395. $this->errorMessage = curl_error($ch);
  396. $this->lastRequestInfo = curl_getinfo($ch);
  397. if (is_array($this->lastRequestInfo)) {
  398. $this->httpCode = $this->lastRequestInfo['http_code'];
  399. }
  400. if ($this->errorCode OR $this->errorMessage) {
  401. $this->error = true;
  402. }
  403. curl_close($ch);
  404. if ($this->verboseLogON) {
  405. rewind($this->verboseLogStream);
  406. file_put_contents($this->verboseLogFilePath, stream_get_contents($this->verboseLogStream), 8);
  407. file_put_contents($this->verboseLogFilePath, print_r($this->lastRequestInfo(), true), 8);
  408. fclose($this->verboseLogStream);
  409. }
  410. } else {
  411. throw new Exception('SHINDEIRU_URL_EMPTY');
  412. }
  413. return($result);
  414. }
  415. /**
  416. * Returns current error state as empty or not array
  417. *
  418. * @return array
  419. */
  420. public function error() {
  421. $result = array();
  422. if ($this->error) {
  423. $result['errorcode'] = $this->errorCode;
  424. $result['errormessage'] = $this->errorMessage;
  425. }
  426. return($result);
  427. }
  428. /**
  429. * Returns last request http code. 0 - on fail.
  430. *
  431. * @return int
  432. */
  433. public function httpCode() {
  434. return($this->httpCode);
  435. }
  436. /**
  437. * Returns last request full info
  438. *
  439. * @return array/bool
  440. */
  441. public function lastRequestInfo() {
  442. return($this->lastRequestInfo);
  443. }
  444. /**
  445. * Sets user agent for current instance
  446. *
  447. * @param string $userAgent
  448. *
  449. * @return void
  450. */
  451. public function setUserAgent($userAgent) {
  452. if (!empty($userAgent)) {
  453. $this->userAgent = $userAgent;
  454. $this->setOpt(CURLOPT_USERAGENT, $this->userAgent);
  455. }
  456. }
  457. /**
  458. * Sets instance connection timeout in seconds
  459. *
  460. * @param int $timeout
  461. *
  462. * @return void
  463. */
  464. public function setTimeout($timeout) {
  465. $timeout = preg_replace("#[^0-9]#Uis", '', $timeout);
  466. if (!empty($timeout)) {
  467. $this->timeout = $timeout;
  468. $this->setOpt(CURLOPT_CONNECTTIMEOUT, $this->timeout);
  469. }
  470. }
  471. /**
  472. * Sets HTTP basic auth params
  473. *
  474. * @param string $login
  475. * @param string $password
  476. *
  477. * @return void
  478. */
  479. public function setBasicAuth($login, $password) {
  480. $this->setOpt(CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  481. $this->setOpt(CURLOPT_USERPWD, $login . ':' . $password);
  482. }
  483. /**
  484. * $verboseLogON setter
  485. *
  486. * @param $state
  487. *
  488. * @return void
  489. */
  490. public function setVerboseLog($state, $logFilePath = '') {
  491. $this->verboseLogON = $state;
  492. $this->verboseLogFilePath = empty($logFilePath) ? self::DEFAULT_VERBOSE_LOG_PATH : $logFilePath;
  493. }
  494. }