php实现的Curl封装类Curl.class.php用法实例分析

这篇文章主要介绍了php实现的Curl封装类Curl.class.php用法,以完整实例形式较为详细的分析了Curl封装类的定义及相关使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下。

本文实例讲述了php实现的Curl封装类Curl.class.php用法,分享给大家供大家参考,具体如下:

  1. <?php
  2. //curl类
  3. class Curl
  4. {
  5. function Curl(){
  6. return true;
  7. }
  8. function execute($method, $url, $fields='', $userAgent='', $httpHeaders='', $username='', $password=''){
  9. $ch = Curl::create();
  10. if(false === $ch){
  11. return false;
  12. }
  13. if(is_string($url) && strlen($url)){
  14. $ret = curl_setopt($ch, CURLOPT_URL, $url);
  15. }else{
  16. return false;
  17. }
  18. //是否显示头部信息
  19. curl_setopt($ch, CURLOPT_HEADER, false);
  20. //
  21. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  22. if($username != ''){
  23. curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
  24. }
  25. $method = strtolower($method);
  26. if('post' == $method){
  27. curl_setopt($ch, CURLOPT_POST, true);
  28. if(is_array($fields)){
  29. $sets = array();
  30. foreach ($fields AS $key => $val){
  31. $sets[] = $key . '=' . urlencode($val);
  32. }
  33. $fields = implode('&',$sets);
  34. }
  35. curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
  36. }else if('put' == $method){
  37. curl_setopt($ch, CURLOPT_PUT, true);
  38. }
  39. //curl_setopt($ch, CURLOPT_PROGRESS, true);
  40. //curl_setopt($ch, CURLOPT_VERBOSE, true);
  41. //curl_setopt($ch, CURLOPT_MUTE, false);
  42. curl_setopt($ch, CURLOPT_TIMEOUT, 10);//设置curl超时秒数
  43. if(strlen($userAgent)){
  44. curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
  45. }
  46. if(is_array($httpHeaders)){
  47. curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);
  48. }
  49. $ret = curl_exec($ch);
  50. if(curl_errno($ch)){
  51. curl_close($ch);
  52. return array(curl_error($ch), curl_errno($ch));
  53. }else{
  54. curl_close($ch);
  55. if(!is_string($ret) || !strlen($ret)){
  56. return false;
  57. }
  58. return $ret;
  59. }
  60. }
  61. function post($url, $fields, $userAgent = '', $httpHeaders = '', $username = '', $password = ''){
  62. $ret = Curl::execute('POST', $url, $fields, $userAgent, $httpHeaders, $username, $password);
  63. if(false === $ret){
  64. return false;
  65. }
  66. if(is_array($ret)){
  67. return false;
  68. }
  69. return $ret;
  70. }
  71. function get($url, $userAgent = '', $httpHeaders = '', $username = '', $password = ''){
  72. $ret = Curl::execute('GET', $url, '', $userAgent, $httpHeaders, $username, $password);
  73. if(false === $ret){
  74. return false;
  75. }
  76. if(is_array($ret)){
  77. return false;
  78. }
  79. return $ret;
  80. }
  81. function create(){
  82. $ch = null;
  83. if(!function_exists('curl_init')){
  84. return false;
  85. }
  86. $ch = curl_init();
  87. if(!is_resource($ch)){
  88. return false;
  89. }
  90. return $ch;
  91. }
  92. }
  93. ?>

GET用法:

$curl = new Curl();

$curl->get('http://www.phpfensi.com/');

POST用法:

$curl = new Curl();

$curl->get('http://www.phpfensi.com/', 'p=1&time=0');

希望本文所述对大家的php程序设计有所帮助。