2016-08-17 83 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

現在有用,謝謝 –

相關問題