Search code examples
podio

Error in file uploading Podio API


Can someone please help me in uploading file in Podio? I am new in Podio library so I am trying but getting lots of errors.

Warning: realpath() expects parameter 1 to be a valid path, resource given in /home/gphxyz/public_html/decode/podio-php/models/PodioFile.php on line 54

Warning: filesize() expects parameter 1 to be a valid path, resource given in /home/gphxyz/public_html/decode/podio-php/models/PodioFile.php on line 54

Fatal error: Uncaught PodioBadRequestError: "'source' parameter must given as multipart/form-data with type 'file'" 
Request URL: http://api.podio.com/file/v2/ 
Stack Trace: #0 /home/gphxyz/public_html/decode/podio-php/lib/Podio.php(352): Podio::request('POST', '/file/v2/', Array, Array) 
             #1 /home/gphxyz/public_html/decode/podio-php/models/PodioFile.php(54): Podio::post('/file/v2/', Array, Array) 
             #2 /home/gphxyz/public_html/decode/podio-php/index.php(22): PodioFile::upload(Resource id #72, 'http://geeksper...') 
             #3 {main} thrown in /home/gphxyz/public_html/decode/podio-php/lib/Podio.php on line 289

My code is below:

<?php
require_once 'PodioAPI.php';

//Initalize Podio connection
$client_id = ''; 
$client_secret = ""; 

Podio::setup($client_id, $client_secret);

//App ID's
$opname_app_id = '21209880';
$opname_app_token = "";  

Podio::authenticate_with_app($opname_app_id, $opname_app_token);
$opname_auth = Podio::$oauth;

$filepath = 'http://geeksperhour.xyz/decode/podio-php/credit.jpg';
$filename = 'credit.jpg';

$goFile = PodioFile::upload($filepath, $filename);
$fileID = $goFile->file_id;
print_r($fileID);

Solution

  • You might find that lib/podio.php for file uploads is deprecated since a while.

    See the open Ticket on Github: The usage of the @filename API for file uploading is deprecated - File upload #74

    Changing the API in line 189 will allow you to follow the documentation again.

    from

     if (!empty($options['upload'])) {
          curl_setopt(self::$ch, CURLOPT_POST, TRUE);
          curl_setopt(self::$ch, CURLOPT_SAFE_UPLOAD, FALSE);
          curl_setopt(self::$ch, CURLOPT_POSTFIELDS, $attributes);
          self::$headers['Content-type'] = 'multipart/form-data';
        } 
    

    to

       if (!empty($options['upload'])) {
          $cfile = curl_file_create(substr($attributes[ "source" ], 1));
          // Assign POST data
          $attributes[ "source" ] = $cfile;
          curl_setopt(self::$ch, CURLOPT_POST, TRUE);
          curl_setopt(self::$ch, CURLOPT_POSTFIELDS, $attributes);
          self::$headers['Content-type'] = 'multipart/form-data';
      }
    

    Worked for me in a PHP 7.2 Ubuntu 16.04 environment.

    Also make sure the path to your file is pointing to local a path of your server.

    Additional if you use composer you might find it useful to point to the master rather than the latest release:

    composer require podio/podio-php:dev-master