如何修复 'CURLFile' function not found 错误?

How to fix 'CURLFile' function not found error?

我正在尝试实施 marketo create file rest API。但由于我的 php 版本,我收到 'Class CURLFile not found' 错误。所以请帮助我如何在较低的 php 中使用 'CURLFile' 功能,或者它们是否是相同功能的任何另一个等价物。请检查我的以下代码:-

<?php
$file = new CreateFile();
$file->file = new CURLFile("File.txt", "text/plain", "file");
$file->folder = new stdClass();
$file->folder->id = 5565;
$file->folder->type = "Folder";
$file->name = "Test File.txt";
print_r($file->postData());

class CreateFile{
    private $host = "CHANGE ME";
    private $clientId = "CHANGE ME";
    private $clientSecret = "CHANGE ME";
    public $name;//name of file to create, required
    public $file;//CURLFile of file to input
    public $folder;//json object with two members, id and type(Folder or Program)
    public $description;//option description of file
    public $insertOnly;//boolean option to only perform an Insert

    public function postData(){
        $url = $this->host . "/rest/asset/v1/files.json?access_token=" . $this->getToken();
        $ch = curl_init($url);
        $requestBody = $this->bodyBuilder();
        curl_setopt($ch,  CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/json','Content-Type: multipart/form-data'));
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
        curl_getinfo($ch);
        $response = curl_exec($ch);
        return $response;
    }

    private function getToken(){
        $ch = curl_init($this->host . "/identity/oauth/token?grant_type=client_credentials&client_id=" . $this->clientId . "&client_secret=" . $this->clientSecret);
        curl_setopt($ch,  CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/json',));
        $response = json_decode(curl_exec($ch));
        curl_close($ch);
        $token = $response->access_token;
        return $token;
    }
    private function bodyBuilder(){
        $requestBody = array("file" => $this->file, "name" => $this->name, "folder" => json_encode($this->folder));
        if (isset($this->description)){
            $requestBody["description"] = $this->description;
        }
        if(isset($this->insertOnly)){
            $requestBody["insertOnly"] = $this->insertOnly;
        }
        return $requestBody;
    }   
}

替换$file->file = new CURLFile("File.txt", "text/plain", "file");

$file->file = "@File.txt;filename=file;type=text/plain";

在php 5.5+你需要设置curl_setopt ($ch, CURLOPT_SAFE_UPLOAD, false);

PHP 通过可加载模块提供某些功能。对于需要额外库的东西,比如 libcurl,最常见的情况就是这种情况。为了使用该功能,需要在您的系统上安装相关模块,并且加载相关模块的语句需要出现在您的 php.ini 文件中。

如何安装 curl 模块取决于您的安装方式 PHP;它可能就像安装软件包一样简单,也可能需要您重新编译所有 PHP.

检查你的 PHP 版本。 CURLFile 仅适用于 PHP 版本 => 5.5

如果您使用的是 PHP 版本 >=5.5 和类似 Laravel 的框架,请记得在 CURLFILE 之前添加“\”。

文件上传 w/o PHP 版本 < 5.5.0 中的 CURLFile Class 不工作。将 PHP 版本更新为 5.5、5.6、7.0、7.1。然后就可以了。

new CURLFile("File.txt", "text/plain", "file");

  1. 创建 CURLFile 对象时需要 \,因为没有 \ 它会在本地而不是全局查找此命名空间。

  2. 第一个参数需要是服务器的路径。

    例如:/var/www/html/File.text

    您可以使用 PHP realpath 函数为您附加 /var/www/html/ 部分。


解决方案

$file_server_path = realpath(File.text);
$file->file = new \CURLFile($file_server_path, 'text/plain', 'file');