首先,确保您的PHP环境已安装cURL扩展。然后,您可以按照以下步骤使用array_map()函数来实现一个简单的cURL函数:
function curlRequest($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$urls = [
'https://example.com/api1',
'https://example.com/api2',
'https://example.com/api3'
];
$responses = array_map('curlRequest', $urls);
print_r($responses);
完整示例代码如下:
function curlRequest($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$urls = [
'https://example.com/api1',
'https://example.com/api2',
'https://example.com/api3'
];
$responses = array_map('curlRequest', $urls);
print_r($responses);
这样,您就可以使用array_map()函数来简单地执行多个cURL请求,并将结果存储在一个数组中。