1

I've been working on building an API as a learning purpose. It is essentially a twitter-like micro post API. Alongside the API I've been building a client web application powered off of the API. I am able to successfully post statuses without any problems. But now I am trying to allow users to upload an image along with the post. The client application is built using the mvc pattern. I post the submitted post to my post controller via jQuery ajax like so:

$.ajax({
            url: '../../posts/newPost',
            type: 'POST',
            data: formData,
            async: true,
            success: function (data) {
                console.log(data);
                $('#post-form-area').removeClass('loading');
                $('#post-body').val('');
                $('#files').val('');
                $('#attached').find('img').remove();
                $('#attached').hide();

            },
            cache: false,
            contentType: false,
            processData: false
        });

I am able to successfully get the file data using the $_FILES array within my controller. However, I am having a lot of difficulty figuring out how to send this to my RESTful API. I created a class to handle my API calls and here is the code for my POST method:

$ds = json_encode($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $ds);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//execute the request
$result = curl_exec($ch);

Here is the data that I pass to post a text only status:

$data = array('user_id' => $_SESSION['user_id'], 'body' => $body, 'time' => $timestamp);

I have tried passing $_FILES['file']['tmp_name'] as part of the array, but on API side the $_FILES array is null. I tried following this example, but I ran in to the same problem.

The way my API is set up, if the $_SERVER['request_method'] is POST I get the contents from cURL from php://input and then pass these parameters to the necessary controller. Do I need to add anything here that will give me access to the $_FILES array? I've been digging for about 3 hours now and haven't been able to find anything that has helped me, so I appreciate any help.

Thanks!

Edit: I don't see how this is a duplicate as the link you posted that it is a duplicate of has nothing to do with uploading an image through a RESTful service...

Edit 2: I've narrowed it down to how I am sending the data from my client application because I am successfully able to use the following command line code to upload a file:

curl -X POST -F "image=@test.JPG" http://rest.mattaltepeter.com/posts/upload

And then the code to receive it:

public function uploadAction() {

        $result['success'] = move_uploaded_file($_FILES['image']['tmp_name'], SITE_ROOT . '/uploads/' . $_FILES['image']['name']);
        $result['data'] = UPLOAD_DIR . $_FILES['image']['name'];
        return $result;
    }

I came across this tutorial and have been able to implement it fine with a static URL to an image, but that isn't very helpful as I want a user to be able to select their own image to upload with their post. So it looks like my issue is revolved around getting the full path as that tutorial mentions on line 4 of the first code block and sending it to the api. So how do I get that same information from the $_FILES array instead of a static file URL?

Community
  • 1
  • 1
Matt Altepeter
  • 956
  • 1
  • 8
  • 18
  • How is this a duplicate? The link posted only covers uploading via AJAX to the same server and has nothing to do with passing the user-selected file to a RESTful web service – Matt Altepeter Jan 09 '15 at 07:59
  • You should remove the part about ajax upload, it's irrevelant. – Marek Jan 09 '15 at 08:07

1 Answers1

1

Finally figured it out. It had to do with using json_encode() to send my POST data. As soon as I tried it with out json_encode(), I had access to the $_FILES array. I think I'll have to add a parameter to my ApiCall->post() method to tell it if I want to json_encode() the data I wish to pass.

Matt Altepeter
  • 956
  • 1
  • 8
  • 18