2016-08-17 55 views
0

我正在尝试实施marketo创建文件rest API。但由于我的PHP版本,我得到'CURLFile not found'错误。所以请帮助我如何在低级php中使用'CURLFile'函数,或者是他们的任何另一个相同的函数。请检查我下面的代码: -如何解决'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; 
    } 
} 

回答

2

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

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

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

+0

工程!谢谢。这就像老版本php的polyfill一样。 –

0

PHP提供了可加载模块的方式一定的功能。对于需要额外库(如libcurl)的情况,这是最常见的情况。为了使用该功能,需要在系统上安装相关模块,并且需要在您的php.ini文件中显示加载相关模块的语句。

如何安装curl模块取决于您如何安装PHP;它可能与安装软件包一样简单,或者可能需要您重新编译所有PHP。

1

检查你的PHP版本。 CURLFile只适用于PHP版本=> 5.5

+0

现在有用,谢谢 –

相关问题