Need API CURL version

Hi all, 
I need API curl version, because my hosting does not support fsocket. Where can I download the API. 

Thanks for the help ^_^

5replies Oldest first
  • Oldest first
  • Newest first
  • Active threads
  • Popular
  • Our API uses standard HTTP requests. You can access it via any language and directly on the command line via CURL. We use CURL on the command line internally to test some fringe use cases.

     

    More specifically, what langage are you using?

    Guessing, I think fsocket might be a PHP thing. If you download the PHP client and rewrite the client to use curl you should only have to update a couple functions to get it to work.

    See:

    https://github.com/smartfile/client-php/blob/master/Services/SmartFile/Client.php

    doRequest($uri, $method, $data=null, $extra_headers=array())

    _request($uri, $method, $data=null, $extra_headers=array())

    Reply Like
  • This my code

    class CurlUpload {
        private $header;
        private $return;
        public function __construct($key = '', $password = '') {
            $this->return    = array(
                'error'        => 0,
                'message'    => ''
            );
            $auth             = base64_encode($key . ':' . $password);
            $this->header    = 'Authorization: Basic ' . $auth . "\r\n";
        }
        public function upload($file = '') {
            if(!empty($file)) {
                if($file['type'] == 'image/jpeg') {
                    $filename = date('YmdHis') . '.' . $file['type'];
                    $filedata = $file['tmp_name'];
                    $filesize = $file['size'];
                    $ch = curl_init();
                    $headers = array("Content-Type: multipart/form-data", "User-Agent: SmartFile PHP API client v2.1\r\n", $this->header);
                    $postfields = array("filedata" => $filedata, "filename" => $filename);
                    $options = array(
                        CURLOPT_URL => 'http://irul.smartfile.com/api/2/path/data/',
                        CURLOPT_HEADER => true,
                        CURLOPT_POST => 1,
                        CURLOPT_HTTPHEADER => $headers,
                        CURLOPT_POSTFIELDS => $postfields,
                        CURLOPT_INFILESIZE => $filesize,
                        CURLOPT_RETURNTRANSFER => true
                    ); // cURL options
                    curl_setopt_array($ch, $options);
                    var_dump(curl_exec($ch));
                    $this->return['message']    = 'image was uploaded';
                } else {
                    $this->return['error']         = 1;
                    $this->return['message']    = 'only allow jpg/jpeg';
                }
            } else {
                $this->return['error']         = 1;
                $this->return['message']    = 'image required';
            }
            echo json_encode($this->return);
        }
    

    Sample for use the code 

    $curlUpload = new curlUpload({KEY}, {PASS});
    $curlUpload->upload($_FILES['image']);
    

    But return

    string(292) "HTTP/1.1 400 Bad Request
    Server: nginx
    Date: Mon, 22 Sep 2014 16:13:14 GMT
    Content-Type: text/html
    Content-Length: 166
    
    <html>
    <head><title>400 Bad Request</title></head>
    <body bgcolor="white">
    <center><h1>400 Bad Request</h1></center>
    <hr><center>nginx</center>
    </body>
    </html>
    

    Please correct my code if im wrong.. Thanks before.. ^_^

    Reply Like
  • A couple things worth checking.

    • //Try:
      $postfields = array("filedata" => "@$filedata", "filename" => $filename);
      

    http://www.php-guru.in/2013/upload-files-using-php-curl/

    • 400 is supposed to be returned when a file is not included in the request. Check your curl to make sure it includes the file. It seems there is a difference between php 5.5 and previous versions.

    http://www.58bits.com/blog/2014/06/14/how-upload-file-using-php-curl

    http://code.stephenmorley.org/php/sending-files-using-curl/

     

    • Multi-part form data may require a bountry in your curl request.
    • Curl could require some options for obscure things like following redirects or SSL.
    Reply Like
  • Yeaaahh, my code is run.. ^_^

    <?php
    class CurlUpload {
        private $header;
        private $return;
        public function __construct($key = '', $password = '') {
            $this->return    = array(
                'error'        => 0,
                'message'    => '',
                'filename'    => ''
            );
            $auth             = base64_encode($key . ':' . $password);
            $this->header    = 'Authorization: Basic ' . $auth;
        }
        public function upload($file = '', $directory = '') {
            if(!empty($file)) {
                if($file['type'] == 'image/jpeg') {
                    $filename             = date('YmdHis') . '.jpg';
                    $filedata             = $file['tmp_name'];
                    /*** Upload to local server directory temp/ */
                    $local_upload         = move_uploaded_file($filedata,'temp/' . $filename);
                    if($local_upload === true) {
                        $ch             = curl_init();
                        curl_setopt($ch, CURLOPT_URL, 'http://irul.smartfile.com/api/2/path/data/' . $directory);
                        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
                        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
                        $headers = array(
                            'User-Agent: SmartFile PHP API client v2.1',
                            $this->header
                        );
                        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                        curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => "@" . realpath('temp/' . $filename)));
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                        curl_setopt($ch, CURLOPT_HEADER, false);
                        curl_exec($ch);
                        if(curl_exec($ch) === false) {
                            $this->return['error']         = 1;
                            $this->return['message']    = 'Curl error: ' . curl_error($ch);
                        } else {
                            $info         = curl_getinfo($ch);
                            if($info['http_code'] == '200') {
                                unlink(realpath('temp/' . $filename));
                                $this->return['message']    = 'image was uploaded';
                                $this->return['filename']    = $directory . '/' . $filename;
                            }
                        }
                    } else {
                        $this->return['error']         = 1;
                        $this->return['message']    = 'upload to local server was failed';
                    }
                } else {
                    $this->return['error']         = 1;
                    $this->return['message']    = 'only allow jpg/jpeg image';
                }
            } else {
                $this->return['error']         = 1;
                $this->return['message']    = 'image required';
            }
            echo json_encode($this->return);
        }
    }
    

    And i try use that code 

    require_once '../library/smartfile/curlUpload.php';
    $curlUpload = new curlUpload(KEY, PASS);
    $curlUpload->upload($_FILES['image'], 'ecommerce');
    

    And return yeaaaahh..

    {
        "error": 0,
        "message": "image was uploaded",
        "filename": "ecommerce/20140923001642.jpg"
    }
    

    Thanks for u, and this link http://smartfile.forumbee.com/t/8121s .. ^_^

    Reply Like
  • Glad you got it worked out. Thanks for linking the other post for others who may stumble upon this article.

    Reply Like
Like Follow
  • 5 yrs agoLast active
  • 5Replies
  • 956Views
  • 2 Following