ホームページ  >  記事  >  バックエンド開発  >  Curl カプセル化クラス Curl.class.php と呼び出しメソッド

Curl カプセル化クラス Curl.class.php と呼び出しメソッド

WBOY
WBOYオリジナル
2016-07-25 08:54:35987ブラウズ
  1. //curl类
  2. class Curl
  3. {
  4. function Curl(){
  5. return true;
  6. }
  7. functionexecute($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 = strto lower($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、ポスト用法

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


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。