PHP通过curl上传客户端提交的文件到远程服务器
$ch = curl_init('http://127.0.0.0/upload');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
  'file' => new \CURLFile($_FILES['file']['tmp_name'])
]);

$result = curl_exec($ch);

if ($result === false) {
  $this->error("upload - FAILED");
}

$res = json_decode($result, true);
curl error 28 while downloading https://repo.packagist.org/packages.json: Failed to connect to repo.packagist.org port 443: Connection timed out

更换国内的数据源

 

composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
php cURL error 60: SSL certificate problem: unable to get local issuer certificate问题

这是curl访问https协议时出现的错误,解决方法如下,

  1. 访问bagder/ca-bundle at e9175fec5d0c4d42de24ed6d84a06d504d5e5a09 (github.com)下载crt文件,并保存在准备好的目录中。
  2. 修改php.ini文件,查找curl.cainfo并修改弃置,curl.cainfo=步骤1中crt文件路径(比如:curl.cainfo=C:\d\source\php\ssl\ca-bundle.crt)
  3. 重启php服务即可。

 

pem文件也可以在以下网站下载

curl - Extract CA Certs from Mozilla

 

 

如果curl请求是通过php代码发送的,可以添加以下代码禁用ssl验证即可。

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
使用php curl CURLOPT_WRITEFUNCTION的示例代码

当使用 PHP 的 cURL 库时,可以使用 CURLOPT_WRITEFUNCTION 选项来指定一个回调函数,该函数将在每次接收到的数据块传输到目标时被调用。以下是一个使用 CURLOPT_WRITEFUNCTION 的示例代码:

<?php

// 目标 URL
$url = 'http://example.com';

// 初始化 cURL
$ch = curl_init();

// 设置 cURL 选项
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'writeData');

// 执行 cURL 请求
$response = curl_exec($ch);

// 关闭 cURL 资源
curl_close($ch);

// 处理响应数据
echo $response;

// 回调函数,用于处理接收到的数据块
function writeData($handle, $data) {
    // 在这里可以对接收到的数据进行处理,例如输出到日志文件或进行其他操作
    // 这里仅简单地输出接收到的数据块
    echo 'Received data: ' . $data;
    return strlen($data); // 返回实际接收到的数据长度,以便 cURL 知道下一个数据块的大小
}
?>

在上述示例中,CURLOPT_WRITEFUNCTION 被设置为 'writeData',这将调用 writeData 函数作为回调函数。在 writeData 函数中,可以对接收到的数据进行处理,这里只是简单地输出接收到的数据块。函数返回实际接收到的数据长度,以便 cURL 知道下一个数据块的大小。最后,通过 curl_exec 执行 cURL 请求并获取响应数据。

  • 当前日期:
  • 北京时间:
  • 时间戳:
  • 今年的第:18周
  • 我的 IP:3.15.27.146
农历
五行
冲煞
彭祖
方位
吉神
凶神
极简任务管理 help
+ 0 0 0
Task Idea Collect