How to fix 'CURLFile' function not found error?

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;





5 Answers
5



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


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



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


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



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


curl_setopt ($ch, CURLOPT_SAFE_UPLOAD, false);






Works ! Thank you. This is like a polyfill for older php versions.

– Andrei Telteu
Nov 10 '17 at 12:17



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






It works now, thank you

– Ams Naser
Feb 22 '18 at 11:55



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.


php.ini



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.



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



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.



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)