PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/README.md

https://github.com/betashepherd/php-curl-class
Markdown | 145 lines | 120 code | 25 blank | 0 comment | 0 complexity | a397f3bd99a8bc1f2cbeef82a80620fd MD5 | raw file
Possible License(s): Unlicense
  1. # php-curl-class
  2. [![Build Status](https://travis-ci.org/php-curl-class/php-curl-class.png?branch=master)](https://travis-ci.org/php-curl-class/php-curl-class)
  3. PHP Curl Class is an object-oriented wrapper of the PHP cURL extension.
  4. ### Composer
  5. $ composer require php-curl-class/php-curl-class
  6. ### Quick Start and Examples
  7. ```php
  8. require 'Curl.php';
  9. use \Curl\Curl;
  10. $curl = new Curl();
  11. $curl->get('http://www.example.com/');
  12. ```
  13. ```php
  14. $curl = new Curl();
  15. $curl->get('http://www.example.com/search', array(
  16. 'q' => 'keyword',
  17. ));
  18. ```
  19. ```php
  20. $curl = new Curl();
  21. $curl->post('http://www.example.com/login/', array(
  22. 'username' => 'myusername',
  23. 'password' => 'mypassword',
  24. ));
  25. ```
  26. ```php
  27. $curl = new Curl();
  28. $curl->setBasicAuthentication('username', 'password');
  29. $curl->setUserAgent('');
  30. $curl->setReferrer('');
  31. $curl->setHeader('X-Requested-With', 'XMLHttpRequest');
  32. $curl->setCookie('key', 'value');
  33. $curl->get('http://www.example.com/');
  34. if ($curl->error) {
  35. echo 'Error: ' . $curl->error_code . ': ' . $curl->error_message;
  36. }
  37. else {
  38. echo $curl->response;
  39. }
  40. var_dump($curl->request_headers);
  41. var_dump($curl->response_headers);
  42. ```
  43. ```php
  44. $curl = new Curl();
  45. $curl->setOpt(CURLOPT_SSL_VERIFYPEER, false);
  46. $curl->get('https://encrypted.example.com/');
  47. ```
  48. ```php
  49. $curl = new Curl();
  50. $curl->put('http://api.example.com/user/', array(
  51. 'first_name' => 'Zach',
  52. 'last_name' => 'Borboa',
  53. ));
  54. ```
  55. ```php
  56. $curl = new Curl();
  57. $curl->patch('http://api.example.com/profile/', array(
  58. 'image' => '@path/to/file.jpg',
  59. ));
  60. ```
  61. ```php
  62. $curl = new Curl();
  63. $curl->patch('http://api.example.com/profile/', array(
  64. 'image' => new CURLFile('path/to/file.jpg'),
  65. ));
  66. ```
  67. ```php
  68. $curl = new Curl();
  69. $curl->delete('http://api.example.com/user/', array(
  70. 'id' => '1234',
  71. ));
  72. ```
  73. ```php
  74. // Enable gzip compression.
  75. $curl = new Curl();
  76. $curl->setOpt(CURLOPT_ENCODING , 'gzip');
  77. $curl->get('https://www.example.com/image.png');
  78. ```
  79. ```php
  80. // Case-insensitive access to headers.
  81. $curl = new Curl();
  82. $curl->get('https://www.example.com/image.png');
  83. echo $curl->response_headers['Content-Type'] . "\n"; // image/png
  84. echo $curl->response_headers['CoNTeNT-TyPE'] . "\n"; // image/png
  85. ```
  86. ```php
  87. $curl->close();
  88. ```
  89. ```php
  90. // Example access to curl object.
  91. curl_set_opt($curl->curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1');
  92. curl_close($curl->curl);
  93. ```
  94. ```php
  95. // Requests in parallel with callback functions.
  96. $curl = new Curl();
  97. $curl->setOpt(CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1');
  98. $curl->success(function($instance) {
  99. echo 'call was successful. response was' . "\n";
  100. echo $instance->response . "\n";
  101. });
  102. $curl->error(function($instance) {
  103. echo 'call was unsuccessful.' . "\n";
  104. echo 'error code:' . $instance->error_code . "\n";
  105. echo 'error message:' . $instance->error_message . "\n";
  106. });
  107. $curl->complete(function($instance) {
  108. echo 'call completed' . "\n";
  109. });
  110. $curl->get(array(
  111. 'https://duckduckgo.com/',
  112. 'https://search.yahoo.com/search',
  113. 'https://www.bing.com/search',
  114. 'http://www.dogpile.com/search/web',
  115. 'https://www.google.com/search',
  116. 'https://www.wolframalpha.com/input/',
  117. ), array(
  118. 'q' => 'hello world',
  119. ));
  120. ```