如何修复“CURLFile"函数未找到错误? [英] How to fix 'CURLFile' function not found error?

查看:41
本文介绍了如何修复“CURLFile"函数未找到错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现市场创建文件休息 API.但是由于我的 php 版本,我收到未找到 Class CURLFile"错误.所以请帮助我如何在较低的 php 中使用 'CURLFile' 功能,或者它们是相同功能的任何其他等价物.请检查我的以下代码:-

I am trying to implement marketo create file rest API. But i am getting 'Class CURLFile not found' error due to my php version. So please help how i can use 'CURLFile' funtion in lower php or is their any another equivalent of same function. Please check my below code:-

<?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;
    }   
}

推荐答案

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

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

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

in php 5.5+ you need to set curl_setopt ($ch, CURLOPT_SAFE_UPLOAD, false);

这篇关于如何修复“CURLFile"函数未找到错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆