分享按钮

在PHP中使用curl库来发送HTTP请求并上传文件

默认分类 / 5381人浏览 / 0人评论

// 文件路径

$file_path = 'https://exxxxxx.com/02604cb3d11ebd4d3a7f1a83828757dd.jpg';

// 请求URL

$url = 'https://developer.toutiao.com/auth/entity/upload_material';

// 初始化curl

$curl = curl_init();

// 设置请求选项

curl_setopt($curl, CURLOPT_URL, $url);

curl_setopt($curl, CURLOPT_POST, true);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);


// 构建请求体

$data = array(

'material_type' => 1007,

'material_file' => new CURLFile($file_path),

);

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

// 设置请求头

$headers = array(

'Content-Type: multipart/form-data',

);

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

// 发送请求并获取响应

$response = curl_exec($curl);

// 检查请求是否成功

if ($response === false) {

echo '请求失败: ' . curl_error($curl);

} else {

echo '请求成功: ' . $response;

}

// 关闭curl

curl_close($curl);


感谢博主,喝杯咖啡~