@@ -22,104 +22,98 @@ use Hhxsv5\PhpMultiCurl\Curl;
22
22
use Hhxsv5\PhpMultiCurl\MultiCurl;
23
23
24
24
//Single http request
25
- $getUrl = 'http://www.weather.com.cn/data/cityinfo/101270101.html';
26
25
$options = [//The custom the curl options
27
26
CURLOPT_TIMEOUT => 10,
28
27
CURLOPT_CONNECTTIMEOUT => 5,
29
28
CURLOPT_USERAGENT => 'Multi-Curl Client V1.0',
30
29
];
30
+
31
31
$c1 = new Curl($options);
32
32
$c1->makeGet($getUrl);
33
- $ret = $c1->exec();
34
- var_dump($ret);
35
- if ($ret) {
36
- //Success
37
- var_dump($c1->getResponse());
38
- } else {
33
+ $response = $c1->exec();
34
+ if ($response->hasError()) {
39
35
//Fail
40
- var_dump($c1->getError());
36
+ var_dump($response->getError());
37
+ } else {
38
+ //Success
39
+ var_dump($response->getHttpCode(), $response->getBody());
41
40
}
42
41
```
43
42
44
43
``` PHP
45
44
//Multi http request
46
- $getUrl = 'http://www.weather.com.cn/data/cityinfo/101270101.html';
47
- $postUrl = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=yourtoken';
48
-
49
- $c1 = new Curl();
50
- $c1->makeGet($getUrl);
51
-
52
45
$c2 = new Curl();
53
- $c2->makePost($postUrl);
46
+ $c2->makeGet($getUrl);
47
+ $c3 = new Curl();
48
+ $c3->makePost($postUrl);
54
49
55
50
$mc = new MultiCurl();
56
-
57
- $mc->addCurls([$c1, $c2]);
58
- $ret = $mc->exec();
59
- var_dump($ret);
60
- if ($ret) {
61
- //Success
62
- var_dump($c1->getResponse(), $c2->getResponse());
51
+ $mc->addCurls([$c2, $c3]);
52
+ $allSuccess = $mc->exec();
53
+ if ($allSuccess) {
54
+ //All success
55
+ var_dump($c2->getResponse()->getBody(), $c3->getResponse()->getBody());
63
56
} else {
64
57
//Some curls failed
65
- var_dump($c1-> getError(), $c2 ->getError());
58
+ var_dump($c2->getResponse()-> getError(), $c3->getResponse() ->getError());
66
59
}
67
60
68
61
//Reuse $mc
69
- $c3 = new Curl();
70
- $c3->makeGet($getUrl);
71
-
72
62
$c4 = new Curl();
73
- $c4->makePost($postUrl );
74
-
75
- $mc->addCurls([$c3, $c4] );
76
- $ret = $ mc->exec( );
77
- var_dump($ret );
78
- if ($ret ) {
79
- //Success
80
- var_dump($c3 ->getResponse(), $c4 ->getResponse());
63
+ $c4->makeGet($getUrl );
64
+ $c5 = new Curl();
65
+ $c5->makePost($postUrl );
66
+ $mc->addCurls([$c4, $c5] );
67
+ $allSuccess = $mc->exec( );
68
+ if ($allSuccess ) {
69
+ //All success
70
+ var_dump($c4 ->getResponse()->getBody() , $c5 ->getResponse()->getBody ());
81
71
} else {
82
72
//Some curls failed
83
- var_dump($c3-> getError(), $c4 ->getError());
73
+ var_dump($c4->getResponse()-> getError(), $c5->getResponse() ->getError());
84
74
}
85
75
```
86
76
87
77
``` PHP
88
78
//Upload file
89
79
$postUrl = 'http://localhost/upload.php';//<?php var_dump($_FILES);
90
- $c = new Curl();
91
- $file1 = new \CURLFile('./olddriver.gif', 'image/gif', 'name1');
80
+
81
+ $options = [//The custom the curl options
82
+ CURLOPT_TIMEOUT => 10,
83
+ CURLOPT_CONNECTTIMEOUT => 5,
84
+ CURLOPT_USERAGENT => 'Multi-Curl Client V1.0',
85
+ ];
86
+ $c = new Curl($options);
87
+ $file1 = new CURLFile('./olddriver.gif', 'image/gif', 'name1');
92
88
$params = ['file1' => $file1];
93
89
$c->makePost($postUrl, $params);
94
- $ret = $c->exec();
95
- var_dump($ret);
96
- if ($ret) {
97
- //Success
98
- var_dump($c->getResponse());
99
- } else {
90
+ $response = $c->exec();
91
+ if ($response->hasError()) {
100
92
//Fail
101
- var_dump($c->getError());
93
+ var_dump($response->getError());
94
+ } else {
95
+ //Success
96
+ var_dump($response->getBody());
102
97
}
103
98
```
104
99
105
100
``` PHP
106
101
//Download file
107
- $fileUrl = 'http://localhost/test.gif';
102
+ $fileUrl = 'http://localhost/test.gif';//<?php var_dump($_FILES);
108
103
$options = [//The custom the curl options
109
104
CURLOPT_TIMEOUT => 3600,//1 hour
110
105
CURLOPT_CONNECTTIMEOUT => 10,
111
106
];
112
107
$c = new Curl($options);
113
108
$c->makeGet($fileUrl);
114
- $ret = $c->exec();
115
- var_dump($ret);
116
- if ($ret) {
109
+ $response = $c->exec();
110
+ if ($response->hasError()) {
111
+ //Fail
112
+ var_dump($response->getError());
113
+ } else {
117
114
//Success
118
115
$targetFile = './a/b/c/test.gif';
119
116
var_dump($c->responseToFile($targetFile));
120
- } else {
121
- //Fail
122
- var_dump($c->getError());
123
117
}
124
118
```
125
119
0 commit comments