PHP CURL Multithreaded Operation code example _php tutorial

Source: Internet
Author: User

PHP CURL multithreaded Operation code example


This article mainly introduces the PHP CURL multithreaded Operation code example, this article directly give the implementation code, the need for friends can refer to the following

How to use:

?

1

2

3

$urls = Array ("http://baidu.com", "http://21andy.com", "http://google.com");

$MP = new Multihttprequest ($urls);

$MP->start ();

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

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

/*

* Curl Multithreading class

* How to use:

* ========================

$urls = Array ("http://baidu.com", "http://dzone.com", "http://google.com");

$MP = new Multihttprequest ($urls);

$MP->start ();

* ========================

*/

Class Multihttprequest {

Public $urls = Array ();

Public $curlopt _header = 1;

Public $method = "GET";

function __construct ($urls = False) {

$this->urls = $urls;

}

function Set_urls ($urls) {

$this->urls = $urls;

return $this;

}

function Is_return_header ($b) {

$this->curlopt_header = $b;

return $this;

}

function Set_method ($m) {

$this->medthod = Strtoupper ($m);

return $this;

}

function Start () {

if (!is_array ($this->urls) or count ($this->urls) = = 0) {

return false;

}

$curl = $text = Array ();

$handle = Curl_multi_init ();

foreach ($this->urls as $k = + $v) {

$curl [$k] = $this->add_handle ($handle, $v);

}

$this->exec_handle ($handle);

foreach ($this->urls as $k = + $v) {

Curl_multi_getcontent ($curl [$k]);

echo $curl [$k]. " \ n ";

$text [$k] = Curl_multi_getcontent ($curl [$k]);

Echo $text [$k], "\ n";

Curl_multi_remove_handle ($handle, $curl [$k]);

}

Curl_multi_close ($handle);

}

Private Function Add_handle ($handle, $url) {

$curl = Curl_init ();

curl_setopt ($curl, Curlopt_url, $url);

curl_setopt ($curl, Curlopt_header, $this->curlopt_header);

curl_setopt ($curl, Curlopt_returntransfer, 1);

Curl_multi_add_handle ($handle, $curl);

return $curl;

}

Private Function Exec_handle ($handle) {

$flag = null;

do {

Curl_multi_exec ($handle, $flag);

} while ($flag > 0);

}

}

http://www.bkjia.com/PHPjc/1000087.html www.bkjia.com true http://www.bkjia.com/PHPjc/1000087.html techarticle PHP Curl Multithreaded Operation code example this article mainly introduces the PHP Curl multithreaded Operation code example, this article directly give the implementation code, the need for friends can refer to the following use:?...

  • 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.