PHP code example of hhxsv5 / php-multi-curl
1. Go to this page and download the library: Download hhxsv5/php-multi-curl library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once ('vendor/autoload.php' );
hhxsv5 / php-multi-curl example snippets
use Hhxsv5 \PhpMultiCurl \MultiCurl ;
$getUrl = 'http://www.weather.com.cn/data/cityinfo/101270101.html' ;
$postUrl = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=yourtoken' ;
$options = [
CURLOPT_TIMEOUT => 10 ,
CURLOPT_CONNECTTIMEOUT => 5 ,
CURLOPT_USERAGENT => 'Multi-cURL client v1.5.0' ,
];
$c = new Curl(null , $options);
$c->makeGet($getUrl);
$response = $c->exec();
if ($response->hasError()) {
var_dump($response->getError());
} else {
var_dump($response->getBody());
}
$c->makePost($postUrl);
$response = $c->exec();
if ($response->hasError()) {
var_dump($response->getError());
} else {
var_dump($response->getBody());
}
use Hhxsv5 \PhpMultiCurl \MultiCurl ;
$getUrl = 'http://www.weather.com.cn/data/cityinfo/101270101.html' ;
$postUrl = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=yourtoken' ;
$c2 = new Curl();
$c2->makeGet($getUrl);
$c3 = new Curl();
$c3->makePost($postUrl);
$mc = new MultiCurl();
$mc->addCurls([$c2, $c3]);
$allSuccess = $mc->exec();
if ($allSuccess) {
var_dump($c2->getResponse()->getBody(), $c3->getResponse()->getBody());
} else {
var_dump($c2->getResponse()->getError(), $c3->getResponse()->getError());
}
echo PHP_EOL;
$mc->reset();
$c4 = new Curl();
$c4->makeGet($getUrl);
$c5 = new Curl();
$c5->makePost($postUrl);
$mc->addCurls([$c4, $c5]);
$allSuccess = $mc->exec();
if ($allSuccess) {
var_dump($c4->getResponse()->getBody(), $c5->getResponse()->getBody());
} else {
var_dump($c4->getResponse()->getError(), $c5->getResponse()->getError());
}
$postUrl = 'http://localhost/upload.php' ;
$options = [
CURLOPT_TIMEOUT => 10 ,
CURLOPT_CONNECTTIMEOUT => 5 ,
CURLOPT_USERAGENT => 'Multi-cURL client v1.5.0' ,
];
$c = new Curl(null , $options);
$file1 = new CURLFile('./olddriver.gif' , 'image/gif' , 'name1' );
$params = ['file1' => $file1];
$c->makePost($postUrl, $params);
$response = $c->exec();
if ($response->hasError()) {
var_dump($response->getError());
} else {
var_dump($response->getBody());
}
$fileUrl = 'https://avatars2.githubusercontent.com/u/7278743?s=460&v=4' ;
$options = [
CURLOPT_TIMEOUT => 3600 ,
CURLOPT_CONNECTTIMEOUT => 10 ,
CURLOPT_USERAGENT => 'Multi-cURL client v1.5.0' ,
];
$c = new Curl(null , $options);
$c->makeGet($fileUrl);
$response = $c->exec();
if ($response->hasError()) {
var_dump($response->getError());
} else {
$targetFile = './a/b/c/test.png' ;
var_dump($c->responseToFile($targetFile));
}
bash
composer