Curl wrapper class Curl.class.php and calling method

Source: Internet
Author: User
  1. Curl Class
  2. Class Curl
  3. {
  4. function Curl () {
  5. return true;
  6. }
  7. function Execute ($method, $url, $fields = ', $userAgent = ', $httpHeaders = ', $username = ', $password = ') {
  8. $ch = Curl::create ();
  9. if (false = = = $ch) {
  10. return false;
  11. }
  12. if (is_string ($url) && strlen ($url)) {
  13. $ret = curl_setopt ($ch, Curlopt_url, $url);
  14. }else{
  15. return false;
  16. }
  17. Whether to display header information
  18. curl_setopt ($ch, Curlopt_header, false);
  19. //
  20. curl_setopt ($ch, Curlopt_returntransfer, true);
  21. if ($username! = ") {
  22. curl_setopt ($ch, Curlopt_userpwd, $username. ':' . $password);
  23. }
  24. $method = Strtolower ($method);
  25. if (' post ' = = $method) {
  26. curl_setopt ($ch, Curlopt_post, true);
  27. if (Is_array ($fields)) {
  28. $sets = Array ();
  29. foreach ($fields as $key = = $val) {
  30. $sets [] = $key. '=' . UrlEncode ($val);
  31. }
  32. $fields = Implode (' & ', $sets);
  33. }
  34. curl_setopt ($ch, Curlopt_postfields, $fields);
  35. }else if (' put ' = = $method) {
  36. curl_setopt ($ch, Curlopt_put, true);
  37. }//Bbs.it-home.org
  38. curl_setopt ($ch, curlopt_progress, true);
  39. curl_setopt ($ch, Curlopt_verbose, true);
  40. curl_setopt ($ch, Curlopt_mute, false);
  41. curl_setopt ($ch, Curlopt_timeout, 10);//Set Curl timeout seconds
  42. if (strlen ($userAgent)) {
  43. curl_setopt ($ch, curlopt_useragent, $userAgent);
  44. }
  45. if (Is_array ($httpHeaders)) {
  46. curl_setopt ($ch, Curlopt_httpheader, $httpHeaders);
  47. }
  48. $ret = curl_exec ($ch);
  49. if (Curl_errno ($ch)) {
  50. Curl_close ($ch);
  51. Return Array (Curl_error ($ch), Curl_errno ($ch));
  52. }else{
  53. Curl_close ($ch);
  54. if (!is_string ($ret) | |!strlen ($ret)) {
  55. return false;
  56. }
  57. return $ret;
  58. }
  59. }
  60. Function post ($url, $fields, $userAgent = ", $httpHeaders =", $username = ", $password =") {
  61. $ret = Curl::execute (' POST ', $url, $fields, $userAgent, $httpHeaders, $username, $password);
  62. if (false = = = $ret) {
  63. return false;
  64. }
  65. if (Is_array ($ret)) {
  66. return false;
  67. }
  68. return $ret;
  69. }
  70. function Get ($url, $userAgent = ', $httpHeaders = ', $username = ', $password = ') {
  71. $ret = Curl::execute (' GET ', $url, ', $userAgent, $httpHeaders, $username, $password);
  72. if (false = = = $ret) {
  73. return false;
  74. }
  75. if (Is_array ($ret)) {
  76. return false;
  77. }
  78. return $ret;
  79. }
  80. function Create () {
  81. $ch = null;
  82. if (!function_exists (' Curl_init ')) {
  83. return false;
  84. }
  85. $ch = Curl_init ();
  86. if (!is_resource ($ch)) {
  87. return false;
  88. }
  89. return $ch;
  90. }
  91. }
  92. ?>
Copy Code

1,get usage:

    1. $curl = new curl ();
    2. $curl->get (' http://bbs.it-home.org/');
Copy Code

2,post usage

    1. $curl = new curl ();
    2. $curl->get (' http://bbs.it-home.org/', ' p=1&time=0′ ');
Copy Code
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.