PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/docs/source/index.md

https://github.com/betashepherd/php-curl-class
Markdown | 265 lines | 205 code | 60 blank | 0 comment | 0 complexity | 249a07e0e1e1f0cdef2e0a339d4e65c4 MD5 | raw file
Possible License(s): Unlicense
  1. ---
  2. title: PHP Curl Class Documentation
  3. language_tabs:
  4. - php
  5. toc_footers:
  6. - <a href="https://github.com/php-curl-class/php-curl-class" target="_blank">PHP Curl Class on Github</a>
  7. ---
  8. # Introduction
  9. PHP Curl Class is an object-oriented wrapper of the PHP cURL extension.
  10. # Install
  11. ## Composer
  12. Install using Composer.
  13. ```shell
  14. $ composer require php-curl-class/php-curl-class
  15. ```
  16. ## Include
  17. Include PHP Curl Class.
  18. ```php
  19. <?php
  20. require 'Curl/Curl.php';
  21. use \Curl\Curl;
  22. ```
  23. # Usage
  24. ## GET
  25. ```php
  26. <?php
  27. require 'Curl/Curl.php';
  28. use \Curl\Curl;
  29. $curl = new Curl();
  30. $curl->get('https://www.example.com/', array(
  31. 'q' => 'keyword',
  32. ));
  33. ```
  34. ## POST
  35. ```php
  36. <?php
  37. $curl = new Curl();
  38. $curl->post('https://www.example.com/login/', array(
  39. 'username' => 'myusername',
  40. 'password' => 'mypassword',
  41. ));
  42. ```
  43. ## PUT
  44. ```php
  45. <?php
  46. $curl = new Curl();
  47. $curl->put('https://api.example.com/user/', array(
  48. 'first_name' => 'Zach',
  49. 'last_name' => 'Borboa',
  50. ));
  51. ```
  52. ## PATCH
  53. ```php
  54. <?php
  55. $curl = new Curl();
  56. $curl->patch('https://api.example.com/profile/', array(
  57. 'image' => '@path/to/file.jpg',
  58. ));
  59. ```
  60. ```php
  61. <?php
  62. $curl = new Curl();
  63. $curl->patch('https://api.example.com/profile/', array(
  64. 'image' => new CURLFile('path/to/file.jpg'),
  65. ));
  66. ```
  67. ## DELETE
  68. ```php
  69. <?php
  70. $curl = new Curl();
  71. $curl->delete('https://api.example.com/user/', array(
  72. 'id' => '1234',
  73. ));
  74. ```
  75. ## HEAD
  76. ```php
  77. <?php
  78. $curl = new Curl();
  79. $curl->head('https://www.example.com/song.mp3');
  80. ```
  81. ## OPTIONS
  82. ```php
  83. <?php
  84. $curl = new Curl();
  85. $curl->options('https://www.example.com/');
  86. ```
  87. ## Curl Options
  88. ```php
  89. <?php
  90. $curl = new Curl();
  91. $curl->setOpt(CURLOPT_AUTOREFERER, true);
  92. $curl->get('https://www.example.com/302');
  93. ```
  94. ```php
  95. <?php
  96. $curl = new Curl();
  97. // Enable gzip compression.
  98. $curl->setOpt(CURLOPT_ENCODING , 'gzip');
  99. $curl->get('https://www.example.com/image.png');
  100. ```
  101. ## Authentication
  102. ```php
  103. <?php
  104. $curl = new Curl();
  105. $curl->setBasicAuthentication('username', 'password');
  106. ```
  107. ## User Agent
  108. ```php
  109. <?php
  110. $curl = new Curl();
  111. $curl->setUserAgent('');
  112. ```
  113. ## Referrer
  114. ```php
  115. <?php
  116. $curl = new Curl();
  117. $curl->setReferrer('');
  118. ```
  119. ## Cookies
  120. ```php
  121. <?php
  122. $curl = new Curl();
  123. $curl->setCookie('key', 'value');
  124. ```
  125. ## Request header
  126. ```php
  127. <?php
  128. $curl = new Curl();
  129. $curl->setHeader('X-Requested-With', 'XMLHttpRequest');
  130. $curl->get('https://www.example.com/');
  131. // Case-insensitive access to headers.
  132. echo $curl->request_headers['X-Requested-With'] . "\n"; // XMLHttpRequest
  133. echo $curl->request_headers['x-ReQUeStED-wiTH'] . "\n"; // XMLHttpRequest
  134. ```
  135. ## Response header
  136. ```php
  137. <?php
  138. $curl = new Curl();
  139. $curl->get('https://www.example.com/image.png');
  140. // Case-insensitive access to headers.
  141. echo $curl->response_headers['Content-Type'] . "\n"; // image/png
  142. echo $curl->response_headers['CoNTeNT-TyPE'] . "\n"; // image/png
  143. ```
  144. ## Response body
  145. ```php
  146. <?php
  147. $curl = new Curl();
  148. $curl->get('https://www.example.com/');
  149. echo $curl->response;
  150. ```
  151. ## Curl object
  152. > Example access to curl object.
  153. ```php
  154. <?php
  155. $curl->setOpt(CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1');
  156. // or
  157. curl_set_opt($curl->curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1');
  158. ```
  159. ```php
  160. <?php
  161. $curl->close();
  162. // or
  163. curl_close($curl->curl);
  164. ```
  165. ## Errors
  166. ```php
  167. <?php
  168. $curl = new Curl();
  169. $curl->get('https://www.example.com/404');
  170. if ($curl->error) {
  171. echo 'Error code: ' . $curl->error_code . "\n"; // 404
  172. echo 'Error message: ' . $curl->error_message . "\n"; // HTTP/1.1 404 Not Found
  173. }
  174. else {
  175. echo $curl->response;
  176. }
  177. ```
  178. ## Parallel Requests
  179. ```php
  180. <?php
  181. // Requests in parallel with callback functions.
  182. $curl = new Curl();
  183. $curl->success(function($instance) {
  184. echo 'call was successful. response was' . "\n";
  185. echo $instance->response . "\n";
  186. });
  187. $curl->error(function($instance) {
  188. echo 'call was unsuccessful.' . "\n";
  189. echo 'error code:' . $instance->error_code . "\n";
  190. echo 'error message:' . $instance->error_message . "\n";
  191. });
  192. $curl->complete(function($instance) {
  193. echo 'call completed' . "\n";
  194. });
  195. $curl->get(array(
  196. 'https://duckduckgo.com/',
  197. 'https://search.yahoo.com/search',
  198. 'https://www.bing.com/search',
  199. 'http://www.dogpile.com/search/web',
  200. 'https://www.google.com/search',
  201. 'https://www.wolframalpha.com/input/',
  202. ), array(
  203. 'q' => 'hello world',
  204. ));
  205. ```