【问题标题】:How to fix 'CURLFile' function not found error?如何修复“CURLFile”函数未找到错误?
【发布时间】:2016-12-23 17:37:58
【问题描述】:

我正在尝试实现 marketo 创建文件休息 API。但由于我的 php 版本,我收到“找不到类 CURLFile”错误。所以请帮助我如何在较低的 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;
    }   
}

【问题讨论】:

    标签: php api curl marketo


    【解决方案1】:

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

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

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

    【讨论】:

    • 有效!谢谢你。这就像旧 php 版本的 polyfill。
    • "curl_setopt():不再支持禁用安全上传
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

          在 PHP 版本

          【讨论】:

            【解决方案6】:

            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');
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2013-03-06
              • 2018-10-09
              • 2021-10-09
              • 2021-10-19
              • 2012-06-05
              • 1970-01-01
              • 2022-10-24
              相关资源
              最近更新 更多