Addy
Addy

Reputation: 1080

How to fix 'CURLFile' function not found error?

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

Upvotes: 0

Views: 21502

Answers (6)

No Sssweat
No Sssweat

Reputation: 418

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

  1. Need a \ when creating the CURLFile object as without the \ it looks for this namespace locally rather than globally.

  2. The first param needs to be the server's path.

    Ex: /var/www/html/File.text

    You could use the PHP realpath function to append the /var/www/html/ part for you.


Solution

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

Upvotes: 3

Dinesh Gurjar
Dinesh Gurjar

Reputation: 528

File Upload w/o CURLFile Class in PHP Version < 5.5.0 don´t work. Update Your PHP Version to 5.5, 5.6, 7.0, 7.1. Then it works.

Upvotes: 0

Phil
Phil

Reputation: 1464

If you are using PHP version >=5.5 and a framework like Laravel, remember to add "\" before CURLFILE.

Upvotes: 4

Yusuf
Yusuf

Reputation: 743

Check you PHP version. CURLFile is only work for the PHP version => 5.5

Upvotes: 1

user5588894
user5588894

Reputation:

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

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

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

Upvotes: 6

Wouter Verhelst
Wouter Verhelst

Reputation: 1292

PHP provides certain functionality by way of loadable modules. This is most often the case for things that require extra libraries, like libcurl. In order to use that functionality, the relevant module needs to be installed on your system, and a statement to load the relevant module needs to appear in your php.ini file.

How you install the curl module depends on how you installed PHP; it may be as simple as installing the package, or it may require that you recompile all of PHP.

Upvotes: 0

Related Questions