> 백엔드 개발 > PHP 튜토리얼 > PHP로 구현된 Curl 캡슐화 클래스 Curl.class.php의 사용예 분석

PHP로 구현된 Curl 캡슐화 클래스 Curl.class.php의 사용예 분석

高洛峰
풀어 주다: 2023-03-04 08:06:01
원래의
1559명이 탐색했습니다.

이 기사의 예에서는 PHP에서 구현된 Curl 캡슐화 클래스 Curl.class.php의 사용법을 설명합니다. 참고할 수 있도록 모든 사람과 공유하세요. 자세한 내용은 다음과 같습니다.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

<?php

//curl类

class Curl

{

 function Curl(){

  return true;

 }

 function execute($method, $url, $fields=&#39;&#39;, $userAgent=&#39;&#39;, $httpHeaders=&#39;&#39;, $username=&#39;&#39;, $password=&#39;&#39;){

  $ch = Curl::create();

  if(false === $ch){

   return false;

  }

  if(is_string($url) && strlen($url)){

   $ret = curl_setopt($ch, CURLOPT_URL, $url);

  }else{

   return false;

  }

  //是否显示头部信息

  curl_setopt($ch, CURLOPT_HEADER, false);

  //

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

  if($username != &#39;&#39;){

   curl_setopt($ch, CURLOPT_USERPWD, $username . &#39;:&#39; . $password);

  }

  $method = strtolower($method);

  if(&#39;post&#39; == $method){

   curl_setopt($ch, CURLOPT_POST, true);

   if(is_array($fields)){

    $sets = array();

    foreach ($fields AS $key => $val){

     $sets[] = $key . &#39;=&#39; . urlencode($val);

    }

    $fields = implode(&#39;&&#39;,$sets);

   }

   curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);

  }else if(&#39;put&#39; == $method){

   curl_setopt($ch, CURLOPT_PUT, true);

  }

  //curl_setopt($ch, CURLOPT_PROGRESS, true);

  //curl_setopt($ch, CURLOPT_VERBOSE, true);

  //curl_setopt($ch, CURLOPT_MUTE, false);

  curl_setopt($ch, CURLOPT_TIMEOUT, 10);//设置curl超时秒数

  if(strlen($userAgent)){

   curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);

  }

  if(is_array($httpHeaders)){

   curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);

  }

  $ret = curl_exec($ch);

  if(curl_errno($ch)){

   curl_close($ch);

   return array(curl_error($ch), curl_errno($ch));

  }else{

   curl_close($ch);

   if(!is_string($ret) || !strlen($ret)){

    return false;

   }

   return $ret;

  }

 }

 function post($url, $fields, $userAgent = &#39;&#39;, $httpHeaders = &#39;&#39;, $username = &#39;&#39;, $password = &#39;&#39;){

  $ret = Curl::execute(&#39;POST&#39;, $url, $fields, $userAgent, $httpHeaders, $username, $password);

  if(false === $ret){

   return false;

  }

  if(is_array($ret)){

   return false;

  }

  return $ret;

 }

 function get($url, $userAgent = &#39;&#39;, $httpHeaders = &#39;&#39;, $username = &#39;&#39;, $password = &#39;&#39;){

  $ret = Curl::execute(&#39;GET&#39;, $url, &#39;&#39;, $userAgent, $httpHeaders, $username, $password);

  if(false === $ret){

   return false;

  }

  if(is_array($ret)){

   return false;

  }

  return $ret;

 }

 function create(){

  $ch = null;

  if(!function_exists(&#39;curl_init&#39;)){

   return false;

  }

  $ch = curl_init();

  if(!is_resource($ch)){

   return false;

  }

  return $ch;

 }

}

?>

로그인 후 복사

GET 사용법:

1

2

$curl = new Curl();

$curl->get(&#39;http://www.XXX.com/&#39;);

로그인 후 복사

POST 사용법:

1

2

$curl = new Curl();

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

로그인 후 복사

이 글이 모든 분들의 PHP 프로그래밍에 도움이 되기를 바랍니다. 설계.

PHP에서 구현한 Curl 캡슐화 클래스 Curl.class.php의 사용예 분석에 대한 자세한 관련 글은 PHP 중국어 홈페이지를 주목해주세요!

관련 라벨:
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿