欢迎访问宙启技术站
智能推送

网络请求:PHP中的curl函数详解

发布时间:2023-06-15 11:06:24

PHP的curl函数是一个强大的网络请求库,可以发送各种类型的HTTP请求,并接收响应数据。curl函数可以用于获取API数据、爬取网页、模拟POST请求等多种场景。本文将详细介绍PHP中的curl函数。

1. curl基础

curl_init():初始化一个curl句柄,返回一个curl资源句柄。

curl_setopt():设置curl请求的参数,例如请求方式、请求头、请求体、超时时间等。

curl_exec():执行curl请求,返回响应数据。

curl_close():关闭curl句柄,释放资源。

2. curl请求方式

curl支持多种请求方式,包括GET、POST、PUT、DELETE等。

GET请求:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.example.com/api/user?id=123");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
echo $response;

POST请求:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.example.com/api/user");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "name=Tom&age=18");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
echo $response;

PUT请求:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.example.com/api/user");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, "name=Tom&age=18");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
echo $response;

DELETE请求:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.example.com/api/user?id=123");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
echo $response;

3. curl请求参数

curl请求参数可以通过curl_setopt()函数设置,下面介绍几个常用的参数。

请求头:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.example.com/api/user");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.$token, 'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
echo $response;

请求体:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.example.com/api/user");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array('name'=>'Tom', 'age'=>18)));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.$token, 'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
echo $response;

超时时间:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.example.com/api/user");
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
echo $response;

4. curl异常处理

curl请求可能会发生异常,例如请求超时、请求失败等情况。在使用curl时应该对异常情况进行处理。

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.example.com/api/user");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
if(curl_errno($ch)){
    echo "curl error:".curl_error($ch);
}else{
    echo $response;
}
curl_close($ch);

5. curl并发请求

curl还支持并发请求,可以同时发送多个请求,减少请求时间。

$urls = array("http://www.example.com/api1", "http://www.example.com/api2", "http://www.example.com/api3");
$chs = array();
$mh = curl_multi_init();
foreach($urls as $i => $url){
    $chs[$i] = curl_init();
    curl_setopt($chs[$i], CURLOPT_URL, $url);
    curl_setopt($chs[$i], CURLOPT_RETURNTRANSFER, 1);
    curl_multi_add_handle($mh, $chs[$i]);
}
$running = null;
do{
   curl_multi_exec($mh, $running);
}while($running > 0);
foreach($chs as $i => $ch){
    $response = curl_multi_getcontent($ch);
    curl_multi_remove_handle($mh, $ch);
    echo $response;
}
curl_multi_close($mh);

以上就是PHP中curl函数的详解,通过使用curl函数可以轻松实现网络请求,并且可以通过设置参数对请求进行定制化。