我正在尝试实现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;
}
}
答案 0 :(得分:4)
替换$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);
答案 1 :(得分:1)
检查PHP版本。 CURLFile仅适用于PHP版本=&gt; 5.5
答案 2 :(得分:0)
PHP通过可加载模块提供某些功能。对于需要额外库的事情,例如libcurl,通常就是这种情况。要使用该功能,需要在系统上安装相关模块,并且需要在php.ini
文件中显示加载相关模块的声明。
如何安装curl模块取决于你如何安装PHP;它可能就像安装软件包一样简单,也可能需要重新编译所有PHP。
答案 3 :(得分:0)
如果您使用的是PHP版本> = 5.5和类似Laravel的框架,请记住在CURLFILE之前添加“ \”。
答案 4 :(得分:0)
PHP版本<5.5.0中没有CURLFile类的文件上传不起作用。将您的PHP版本更新为5.5、5.6、7.0、7.1。然后就可以了。
答案 5 :(得分:0)
new CURLFile(“ File.txt”,“ text / plain”,“ file”);
在创建CURLFile对象时需要一个\
,因为没有\
时它将在本地而不是全局地查找此命名空间。
第一个参数必须是服务器的路径。
例如:/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');