Heim  >  Artikel  >  Backend-Entwicklung  >  Curl封装类Curl.class.php及调用方法

Curl封装类Curl.class.php及调用方法

WBOY
WBOYOriginal
2016-07-25 08:54:35989Durchsuche
  1. //curl类
  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. //是否显示头部信息
  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);//设置curl超时秒数
  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. ?>
复制代码

1,GET用法:

  1. $curl = new Curl();
  2. $curl->get(‘http://bbs.it-home.org/’);
复制代码

2,POST用法

  1. $curl = new Curl();
  2. $curl->get(‘http://bbs.it-home.org/’, ‘p=1&time=0′);
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn