Creating an API for my PHP script - Uploading files

Ok, like this: (I’m changing this post content for the third time to better explain)

My site is an Image Hosting website, which means that a user can upload it to my site and get a direct link to their uploaded image.

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

The request to my site should contain the &request=api parameter so that the user receives the plain text returned from my upload.PHP script. Thus, I think that I am providing an easier way to capture data / output from my site.

So, basically AFTER the POST / FILES request from the user's site to my image host , they get the link (s) from my script loading processing, which they should extract and then use for their needs.

My question is: My registered user sends the file to my server WITHOUT reloading the page and returns the URLs of this image. How to do it?


What I tried:

All my attempts were blockages without continuing.

First I added two new columns to two different tables. My users table received an api_key column, which is intended to store the API key, if the user actually signs it. Another is_api column 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 of type TINYINT ), I wanted to verify that the image coming from the user was (or was not) added / added via the API.

A 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 to whom it belongs β†’ Get the user ID based on this key β†’ I save the image on my server β†’ I store image information in my database (now there is a user ID) β†’ I echo outside the URL.

My failures here were:

  • I cannot send ANYTHING from a third-party site to my Image Host asynchronously
  • I could not get anything back to my third-party website.

Why are 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 IMAGE STRING via POST to my server and create the image there myself. I also could not do this, it was just the thought of a guy who managed to think.

So, here it is: My big problem without a solution from me.
If you need more information to help me more easily, please ask.

Thanks.

Update

If I could get the file somehow (asynchronously), I would register it in the database with the is_api field with a value of 1 to mark it as put via the API (external). That way I could create a new file called viewer.php , maybe it will take some parameters as well as viewer.php?request=api&key=_API_KEY_ , and it will return a JSON page with a link to the last image so that the external api user. Retrieving the page via JSON on a third-party website would be quite simple. Thus, with this method, I just need to somehow get the image in my Image Host , and the extracted part will not be too complicated. So, how do I send IMAGE STRING to my image host via POST ?

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

+7
source share
5 answers

I provide a script like this for my users as a bridge to my site:

 <?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 on my website based on the information I receive. I will update this answer as soon as I complete the full CP API on my website, where a webmaster who uses the API can see which images were uploaded through his website to my Image Host. From the script above you can see the API key - it is unique for each user, the one above is just a sample.

For asynchronous work, I made it work by telling users to use the jQuery Form plugin. This is a very convenient plugin for submitting forms, and I show the user how they will do it.

Thanks to everyone who tried to help me.

+5
source

Try using

 json_encode() 

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

 $.getJSON() 

using jQuery on the receiving side. You can then put the resulting link in a div or paragraph (or whatever) that you like. If the hyperlink is an existing hyperlink that is changing / updating, you can change the hyperlink using jQuery .

These links may help:

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

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

+2
source

Please take a look at my RestServer class on GitHub . This will allow you to turn any PHP class into a RESTful web service (returning JSON responses). JSON is a way to go for these types of responses, and RestServer will take care of encoding the returned objects as JSON before returning the data to the client.

You can make GET requests such as http://mysite.com/myapi.php?method=sayHello&name=World , where the query string requires a method name and each parameter for this method. POST requests are also supported by simply changing your request to POST and including your parameters in the POST body.

It will automatically determine the parameters needed for the requested method. You can configure the API secret key and have this as a required parameter for each API method for authentication purposes.

 class Hello { public static function sayHello($name) { // RestServer will json_encode this array before returning it to the client. return array("Response" => "Hello, " . $name); } } $rest = new RestServer(Hello); $rest->handle(); 

Then you just make a call using $. getJSON with jQuery.

+1
source

It looks like they are using a web form, so you should strictly use POST. If you want the client to send you a string (but why do you need to do this?), You can not do without using a script).

If they are POST'ing forms, use the POST method, and then the server side of the script uses PUT to write the backend to the API server, which will return a JSON page with a link to the last image.

If they write directly to the API, follow the same convention, except that your user will use PUT directly (PUT is not available in web forms, just POST and GET).

EDIT: I have a possible solution for a client using HTML5. You can put the image in the html5 canvas and convert the data to a string. Then just grab this line and submit it through the form. See this answer for more information on how to do this.

 <label>Image File:</label><br/> <input type="file" id="imageLoader" name="imageLoader"/> <canvas id="imageCanvas"></canvas> <script> var imageLoader = document.getElementById('imageLoader'); imageLoader.addEventListener('change', handleImage, false); var canvas = document.getElementById('imageCanvas'); var ctx = canvas.getContext('2d'); function handleImage(e){ var reader = new FileReader(); reader.onload = function(event){ var img = new Image(); img.onload = function(){ canvas.width = img.width; canvas.height = img.height; ctx.drawImage(img,0,0); } img.src = event.target.result; } reader.readAsDataURL(e.target.files[0]); /** replace readAsDataURL() with something that will read it to a string. Reference: https://developer.mozilla.org/en-US/docs/DOM/FileReader **/ } </script> 

To summarize: put the image in the canvas DOM element, then convert the canvas data to a string.

0
source

How to get the original request from a third-party site to your image host using your upload.php via POST, and then inside upload.php you redirect the request to another page using an AJAX call. This way it will be asynchronous. I think it's possible.

0
source

All Articles