Jump to content

PCloud abd curl


Go to solution Solved by M.O.S. Studios,

Recommended Posts

Can anyone make heads or tales of this?

I am playing around with pCloud. The Pcloud file upload documentation is kinda vague. they don't explain where or how to attach the file, just how to name it, and where to put it.  They even have instructions on how to send it as binaries. but that is also kind of vague.

 

Can someone see what I am missing? This code is supposed to take a string, create a temp file, and then upload it. at the moment, it uploads a file with the right file name, but  only contains two dashes

        $uploadPath     = 'uploadpath';
        $accessToken    = AccessToken;
        $product_data   = fopen('php://memory', 'r+');
        fputs($product_data, 'content string');
        rewind($product_data);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,            'https://api.pcloud.com/uploadfile' );
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt($ch, CURLOPT_POST,           1 );
        curl_setopt($ch, CURLOPT_POSTFIELDS,     array('filename'=>'test.csv', 'data'=>$product_data)); 
        curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Content-Type: multipart/form-data', 'Authorization: Bearer '.$accessToken)); 

        $result = curl_exec($ch);
        var_export(curl_error($ch));
        var_export($result);
        curl_close($ch);

 

Edited by M.O.S. Studios
Link to comment
https://forums.phpfreaks.com/topic/327814-pcloud-abd-curl/
Share on other sites

  • Solution

So, after giving up on the documentation I gave postman.com a gander.

All the parameters are supposed to be sent within the URL, and the file as the only information in the post field. They also allow you to send plain text, and they will encode it for you.

So here is what I did:

        $uploadPath     = str_replace("/".basename($args['uploadPath']), '', $args['uploadPath']);
        $accessToken    = getPcloudToken();
        $query          = array('filename'=>'file.csv', 'nopartial'=>'1', 'path'=> $uploadPath);
        $product_data   = $args['product_data'];

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,            'https://api.pcloud.com/uploadfile?'.http_build_query($query));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt($ch, CURLOPT_POST,           1 );
        curl_setopt($ch, CURLOPT_POSTFIELDS,     $product_data);
        curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Content-Type: text/plain', 'Authorization: Bearer '.$accessToken)); 

        curl_exec($ch);
        curl_close($ch);

I was able to take the whole temp file creation out. so that was nice

Link to comment
https://forums.phpfreaks.com/topic/327814-pcloud-abd-curl/#findComment-1653936
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.