Php 如何修复“CURLFile”函数未找到错误?
我正在尝试实现marketo创建文件RESTAPI。但是由于我的php版本,我得到了“找不到类文件”错误。因此,请帮助我如何在较低的php中使用“CURLFile”函数,或者它们的任何其他等效函数。请检查我的以下代码:-Php 如何修复“CURLFile”函数未找到错误?,php,api,curl,marketo,Php,Api,Curl,Marketo,我正在尝试实现marketo创建文件RESTAPI。但是由于我的php版本,我得到了“找不到类文件”错误。因此,请帮助我如何在较低的php中使用“CURLFile”函数,或者它们的任何其他等效函数。请检查我的以下代码:- <?php $file = new CreateFile(); $file->file = new CURLFile("File.txt", "text/plain", "file"); $file->folder = new stdClass(); $fil
<?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;
}
}
替换$file->file=new CURLFileFile.txt,text/plain,file
使用$file->file=@file.txt;文件名=文件;类型=文本/纯文本
在PHP5.5+中,您需要设置curl_setopt$ch,CURLOPT_SAFE_UPLOAD,false PHP通过可加载模块提供某些功能。对于需要额外库的东西,如libcurl,最常见的情况就是这种情况。为了使用该功能,需要在系统上安装相关模块,并且需要在php.ini文件中显示一条加载相关模块的语句 如何安装curl模块取决于如何安装PHP;它可能与安装软件包一样简单,也可能需要重新编译所有PHP。检查PHP版本。
CURLFile仅适用于PHP版本=>5.5如果您使用的是PHP版本>=5.5和类似Laravel的框架,请记住在CURLFile之前添加\项。在PHP版本<5.5.0中,文件上载w/o CURLFile类不起作用。将PHP版本更新为5.5、5.6、7.0和7.1。然后它就起作用了 新的CURLFileFile.txt,text/plain,文件 创建CURLFile对象时需要\而不使用\它在本地而不是全局查找此命名空间 第一个参数必须是服务器的路径 Ex:/var/www/html/File.text 您可以使用PHP函数为您附加/var/www/html/part 解决方案
作品非常感谢。这就像旧php版本的polyfill。curl_setopt:不再支持禁用安全上载您救了我!谢谢
$file_server_path = realpath(File.text);
$file->file = new \CURLFile($file_server_path, 'text/plain', 'file');