首页 > 解决方案 > 如何在php中使用curl发送文件

问题描述

我曾尝试使用 curl 发送文件以进行 pdf 转换,但我从 API 端收到错误消息。

    $file_name_with_full_path = WWW_ROOT . "img/IPG_07_12_2020.xlsx";
    if (function_exists('curl_file_create')) {
      $cFile = curl_file_create($file_name_with_full_path);
    } else { 
      $cFile = '@' . realpath($file_name_with_full_path);
    }

    $post = array('file_contents'=> curl_file_create($file_name_with_full_path));
    $curl = curl_init();
    curl_setopt_array($curl, array(
      CURLOPT_URL => "http://localhost:3000/convert/office",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_TIMEOUT => 86400,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_POSTFIELDS =>$post,
      CURLOPT_HTTPHEADER => array('Content-Type: multipart/form-data'),
    ));

    
    
    $result = curl_exec($curl); 

并且错误是 {"message":"context has timed out"}.,请分享您的知识,以正确的方式将文件发送到 curl。提前致谢。

标签: phpcurlcakephppdf-conversion

解决方案


推荐阅读