Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an API for my PHP script - File uploading

Okay here it is: (I am changing this post's content for the third time to explain better)

My website is an Image Hosting website, meaning the user can upload to my website and receive a direct link to his/her uploaded image.

I need some sort of API/way to communicate from my site with my users. I want users that are registered to my website be able to UPLOAD IMAGES from their website to my Image Host without them having to leave their own website (AJAX, iFrame, cURL, JSON, whatever it takes).

The request to my website should include a &request=api paramater so the user gets plain text returned from my upload.php script. This way I think I ensure an easier way of grabbing data/output from my site.

So, basically, AFTER a POST/FILES request from the user's site to my Image Host, they receive link(s) from my upload processing script, which they need to retrieve and then use for their own needs.

My question is: My registered user sends a file to my server WITHOUT reloading the page and gets back the URL(s) to that image. How do I do this?


What I have tried:

All my tries were blockages with no continuing.

First, I added two new columns to two different tables. My users table received an api_key column, which was intended to store an API key if the user actually signs up for it. The other column is_api was added to the table where I store image information, only registered users have their images stored in the database. With this new column (which was type TINYINT) I wanted to check that the image coming from the user was (or was not) added/uploaded via the API.

User sends a request to my Image Host with these parameters: upload.php?submit=true&action=upload&request=api&key=_SOME_API_KEY_. I take the API key and check whom it belongs to -> Retrieve an user id based on that key -> I store the image to my server -> I store image information in my database (have an user id now) -> I echo out the URL.

My failures here were:

  • I couldn't send ANYTHING from the 3rd party website to my Image Host asynchronously
  • I couldn't receive anything back to my 3rd party website.

Why these failures? Because I have no idea how to achieve these two most important steps.

One of my ideas to stop trying to send $_FILES[] to my Image Host, was trying to send an IMAGE STRING via POST to my server and create the image there myself. I couldn't do this either, it was just a thought of a guy who had time to think.

So, this is it: My big problem with no solution from myself.
If you need more information in order to help me out more easily, please ask.

Thank you.

Update

If I just could receive the file somehow (asynchronously) I'd register it in the database with the is_api field with a value of 1, to mark it as put via the API (external). This way, I could create a a new file named viewer.php maybe and it would accept some parameters too like viewer.php?request=api&key=_API_KEY_ and it would return a JSON page with the link to the latest image by that external api user. Retrieving the page via JSON by the 3rd party website would be quite easy. So, with this method, I basically just need to receive the image somehow in my Image Host and the retrieving part wouldn't be too hard. So how would I send an IMAGE STRING to my Image Host via POST?

If this new idea of mine is exploitable, please let me know.

like image 882
aborted Avatar asked Feb 05 '13 22:02

aborted


2 Answers

I am providing a script like this one to my users as a bridge to my website:

<?php 
$api_key = 'n8N9v0g9e7b1h0H4A7s2t6q5K8f07B6E4a5p2k4D6L2T1G4Y7I3z5Q5';
$uses_ajax = false;
$imgit = array('error' => true);

if (isset($_POST['imgit_request']))
{
    if ($_POST['imgit_request'] == 'upload')
    {
        $post_data = array(
            'submit'  => 'true',
            'action'  => 'upload',
            'request' => 'api',
            'api_key' => $api_key,
            'imagestr' => chunk_split(base64_encode(file_get_contents($_FILES['images']['tmp_name']))),
            'imagemime' => $_FILES['images']['type'],
            'imagename' => $_FILES['images']['name'],
            'imagesize' => $_FILES['images']['size'],
        );  
    }
    else if ($_POST['imgit_request'] == 'remote')
    {
        $post_data = array(
            'submit'  => 'true',
            'action'  => 'remote',
            'request' => 'api',
            'api_key' => $api_key,
            'links'   => $_POST['links'],
        );  
    }

    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_URL => 'http://dugi/imgitv3/upload.php',
        CURLOPT_CONNECTTIMEOUT => 5,
        CURLOPT_POST => 1,
        CURLOPT_POSTFIELDS => $post_data,
    ));

    $resp = curl_exec($curl);
    curl_close($curl);

    if (strpos($resp, 'http://') !== false)
    {
        $imgit = array(
            'direct' => $resp,
            'thumb' => str_replace('/i/', '/t/', $resp),
            'error' => false,
        );

        if ($uses_ajax)
        {
            echo $imgit['direct'];  
        }
    }
    if (strpos($resp, 'http://') === false)
    {
        $imgit = array(
            'error' => $resp,
        );  
    }
}
?>

Then I do the rest of the processing in my website based on the information I receive. I will be updating this answer once I finish the complete API CP on my website, where the webmaster who is using the API can see what images have been uploaded via his website to my Image Host. From the script above, you can see an API key - it's unique for every user, the one above is just a sample.

For asynchronous I made it work by telling the users to use the jQuery Form plugin. It's a very handy plugin for submitting forms and I show the user how they would do it.

Thanks for everyone who tried to help me.

like image 108
aborted Avatar answered Nov 03 '22 01:11

aborted


Try using

json_encode()

for the php side (don't forget to include a JSON mimetype header) of the link in question and

$.getJSON()

using jQuery on the receiving end. You can then put the resulting link in the div or paragraph (or whatever) you like. If the hyperlink is an already existing hyperlink being changed/updated, you can change the hyperlink with jQuery.

These links may help:

http://www.w3schools.com/jquery/ajax_getjson.asp

http://php.net/manual/en/function.json-encode.php

like image 34
Josh Austin Avatar answered Nov 03 '22 00:11

Josh Austin