/_includes/GAS/Http/Curl/Request.php

https://github.com/ornicar/mongoPHP · PHP · 231 lines · 123 code · 108 blank · 0 comment · 14 complexity · 085d3ae97854deda811fec14af4cbb88 MD5 · raw file

  1. <?php
  2. class GAS_Http_Curl_Request extends GAS {
  3. protected $_curl;
  4. protected $_uri = null;
  5. protected $_cookies = null;
  6. protected $_post = array();
  7. protected $_get = array();
  8. protected $_curlOpt = array();
  9. protected $_headers = array();
  10. public function __construct( $uri = null, $cookies = true ) {
  11. if( function_exists( 'curl_init' ) !== false ) {
  12. $this->_curl = curl_init();
  13. if( $uri !== null ) {
  14. $this->setUri( $uri );
  15. }
  16. $this->setUserAgent( 'Mozilla/5.0 (X11; U; Linux i686; pl-PL; rv:1.9.0.2) Gecko/20121223 Ubuntu/9.25 (jaunty) Firefox/3.8' );
  17. $this->setSslVerify( false );
  18. $this->setReturnTransfer( true );
  19. $this->setFollowLocation( true );
  20. if( $cookies === true ) {
  21. $this->setCurlOpt( 'CURLOPT_COOKIEFILE', $this->_cookies );
  22. $this->setCurlOpt( 'CURLOPT_COOKIEJAR', $this->_cookies );
  23. }
  24. } else {
  25. throw new GAS_Http_Curl_Exception( 'The cURL extension is required to use this class.' );
  26. }
  27. }
  28. public static function factory( $uri = null, $cookies = true ) {
  29. return new self( $uri, $cookies );
  30. }
  31. public function setUri( $uri ) {
  32. $this->_uri = $uri;
  33. return $this;
  34. }
  35. public function getUri() {
  36. return $this->_uri;
  37. }
  38. public function setHeader( $header, $value ) {
  39. $this->_headers[ $header ] = $value;
  40. return $this;
  41. }
  42. public function setFollowLocation( $bool ) {
  43. $this->setCurlOpt( 'CURLOPT_FOLLOWLOCATION', $bool );
  44. return $this;
  45. }
  46. public function setReturnTransfer( $bool ) {
  47. $this->setCurlOpt( 'CURLOPT_RETURNTRANSFER', $bool );
  48. return $this;
  49. }
  50. public function setSslVerify( $bool ) {
  51. $this->setCurlOpt( 'CURLOPT_SSL_VERIFYPEER', $bool );
  52. return $this;
  53. }
  54. public function setUserAgent( $agent ) {
  55. $this->setCurlOpt( 'CURLOPT_USERAGENT', $agent );
  56. return $this;
  57. }
  58. public function setAuth( $username, $password ) {
  59. $this->setCurlOpt( 'CURLOPT_USERPWD', $username . ':' . $password );
  60. return $this;
  61. }
  62. public function addPost( $key, $value ) {
  63. $this->_post[ $key ] = $value;
  64. return $this;
  65. }
  66. public function addGet( $key, $value ) {
  67. $this->_get[ $key ] = $value;
  68. return $this;
  69. }
  70. public function setPost( $data ) {
  71. $this->_post = $data;
  72. }
  73. public function setGet( $data ) {
  74. $this->_get = $data;
  75. }
  76. public function clearPost() {
  77. $this->_post = array();
  78. return $this;
  79. }
  80. public function clearGet() {
  81. $this->_get = array();
  82. return $this;
  83. }
  84. public function getPost() {
  85. return $this->_post;
  86. }
  87. public function getGet() {
  88. return $this->_get;
  89. }
  90. public function setCurlOpt( $constant, $value ) {
  91. return $this->_curlOpt[ $constant ] = $value;
  92. }
  93. public function sendRequest( $headers = null, $constant = null, $infoConstant = null ) {
  94. $uri = $this->_uri;
  95. if( count( $this->_get ) > 0 ) {
  96. if( strpos( $uri, '?' ) === false ) {
  97. $uri .= '?';
  98. }
  99. $uri .= http_build_query( $this->_get );
  100. }
  101. curl_setopt( $this->_curl, CURLOPT_URL, $uri );
  102. if( ( is_array( $this->_post ) !== false && count( $this->_post ) > 0 ) || ( is_array( $this->_post ) !== true && $this->_post != '' ) ) {
  103. $this->setCurlOpt( 'CURLOPT_POST', true );
  104. $this->setCurlOpt( 'CURLOPT_POSTFIELDS', $this->_post );
  105. }
  106. if( count( $this->_headers ) > 0 ) {
  107. $this->setCurlOpt( 'CURLOPT_HTTPHEADER', $this->_headers );
  108. }
  109. foreach( $this->_curlOpt as $constant => $value ) {
  110. curl_setopt( $this->_curl, constant( $constant ), $value );
  111. }
  112. $response = curl_exec( $this->_curl );
  113. if( $headers !== null ) {
  114. $headers = curl_getinfo( $this->_curl );
  115. return array(
  116. 'http' => $response,
  117. 'headers' => $headers
  118. );
  119. } else {
  120. return $response;
  121. }
  122. }
  123. }