• Welcome to the Chevereto user community!

    Here users from all over the world gather around to learn the latest about Chevereto and contribute with ideas to improve the software.

    Please keep in mind:

    • 😌 This community is user driven. Be polite with other users.
    • 👉 Is required to purchase a Chevereto license to participate in this community (doesn't apply to Pre-sales).
    • 💸 Purchase a Pro Subscription to get access to active software support and faster ticket response times.

[Tutorial] Using the API to upload a file (PHP)

Danny.Domb

👽 Chevereto Freak
Hello guys,

Today I will explain you how to upload a file using the Chevereto 2.0 API.
In fact, this is quite easy.

If you are a total newbie to php and html, I do not recommend to use the API.

First of all, you will need to create a .php file.

I named mine api.php
------------------------------------------------------------------------------------------------------------------------------------------------

So the first thing we need to do, is to create the html form.
This form is like any other upload form.
We call the current file, we set the method to post and because it is a file uploading form, we need to set the encryption type to multipart/form-data

Here is an example:
HTML:
<form action="api.php" method="post" enctype="multipart/form-data">
<input type="file" name="upload" />
<input type="submit" name="submit" />
</form>

------------------------------------------------------------------------------------------------------------------------------------------------
Next is the PHP code.

We must check if the form was submitted

PHP:
<?php
if (isset($_POST['submit']))
{
    //PHP code if the button named submit is pressed.
}
?>

Then since chevereto 2.0 api doesn't return any specific error code depending on the error, I decided to add a very basic validation of the file using the mime types.

We take the uploaded file ($_FILES['upload']) we retreive is type in the array and we compare using the in_array php function it to some values. In the eventuality that the file is invalid, we show and error message and we exit the application.

PHP:
    if (!in_array($_FILES['upload']['type'], array('image/png', 'image/jpg', 'image/jpeg', 'image/gif'))) 
    {
        echo '<span style="color:#CC0000;">Not a valid file type, please upload PNG, JPG, JPEG or GIF images only.</span>';
        exit;
    }

Well, our api.php file is receiving a post request, but it most also send a post request to the api.

If you read carefully the documentation, you will see that you need your website url (if you are posting from a different website), your api key, the image base64_encoded and the return method.

so we will simply define theses with 1 value and 1 array.

You will see that I have "urlencode" my values, I don't think it is necessary, but it is preferable for security reasons.
** I'm not an expert in this domain, this is what I have read.

PHP:
    //Where is the api?
    $url = '[url]http://chevereto.com/2.0/api[/url]';

    // What do we send to chevereto api?
    $fields = array(
                        // The key, in this case my_api_key (the default one)
                        'key' => urlencode('my_api_key'),
                        // The image encoded in base64
                        'upload' => base64_encode(file_get_contents($_FILES['upload']['tmp_name'])),
                        // And how the api answer us?
                        'format' => urlencode('txt')
                    );

Now is the tricky part.
Since we are sending a post request, we must use CURL. Unfortunately, many web server unable it...
If this is the case on your server. You can also create the file without the form on the chevereto server, and instead simply create a html file on your second server. You will need to replace the action.
Instead of api.php you will have to write something like http://yourwebsite.com/api-call.php

Here is the CURL code, I won't explain everything because it is unnecessary.

PHP:
	//open connection
	$ch = curl_init();

	//set the url, number of POST vars, POST data
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_POST, sizeof($fields));
	curl_setopt($ch, CURLOPT_HEADER, false);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_TIMEOUT, 240);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
	curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect: '));

	//execute post
	$result = curl_exec($ch);

	//close connection
	curl_close($ch);

and then we have a $result variable with the answer of the chevereto api.

------------------------------------------------------------------------------------------------------------------------------------------------

You can play with the result now.

You can redirect using
PHP:
header('location '.$result);

or simply print it.
PHP:
print($result);

some useful php methods depending of the api callback:

XML : Simple XML

JSON : JSON decode

------------------------------------------------------------------------------------------------------------------------------------------------

If you have any questions, do not hesitate to post!


Download this tutorial
 
izam2 said:
im a total newbie :(

jk,,

Thank you!

If this is the first thing that you read about the "API" term it could be weird to understand it. But once your understand the term you can easily start to think in great things. For instance, you can make a desktop uploader using the Chevereto API, you can integrate it almost in the exact way that you want in your project. It's a powerful tool ;)
 
Nice1 Danny :)

I've got one question:

with this api.php we get after an upload the directlink of the image... what do i have to change to get the viewer link with a message?

now it looks like this:


and i want to have the viewer link instead of the direct link displayed..

with a message like:

Upload finished, this is your viewer link to BB Code, HTML etc.
 
Avast said:
Nice1 Danny :)

I've got one question:

with this api.php we get after an upload the directlink of the image... what do i have to change to get the viewer link with a message?

now it looks like this:


and i want to have the viewer link instead of the direct link displayed..

with a message like:

Upload finished, this is your viewer link to BB Code, HTML etc.

Well, unfortunately the api doesn't offer a link to the viewer... you could simply create the viewer link... here is a function that will return it.

PHP:
function linkToViewer($link)
{
    $split = explode('/', $link);

    return 'http://'.$split[2].'/?v='.$split[sizeof($split)-1];
}

so after the CURL code you can do the following :
PHP:
$result = linkToViewer($result);


for your second question... you may simply add before the print($result); something similar to :
PHP:
print('Upload Finished.');

and if you want to add like content before the link simply change the print($result); to something similar to :

PHP:
print('[img]'.$result.'[/img]');
 
Thank you, i solced the problem with:

Code:
//close connection
    curl_close($ch);
    echo '<span style="color:#CC0000;">Your Link<br></span>';
    print($result);

Now the api displays us a "viewer" link.... but there is the directory of the images in the link^^
you can see it on this screenshot.
The "i" is my imagedirectory

 
Avast said:
Thank you, i solced the problem with:

Code:
//close connection
    curl_close($ch);
    echo '<span style="color:#CC0000;">Your Link<br></span>';
    print($result);

Now the api displays us a "viewer" link.... but there is the directory of the images in the link^^
you can see it on this screenshot.
The "i" is my imagedirectory


Yeah I didn't think about that, I fixed my functions, but I need to improve it, anyway it should work now.
 
^^ it works, but it kills the website in the link^^



the "i" is still there, but the www.mysite.com is disappeared :p


EDIT://

I found the mistake :)

the function have to be:
Code:
 function linkToViewer($link)
{
    $split = explode('/', $link);
    $img = $split[sizeof($split)-1];
    unset($split[sizeof($split)-1]);
unset($split[sizeof($split)-1]);
 
    return implode('/', $split).'/?v='.$img;
}


this part was wrong

Code:
unset($split[sizeof($split)-2]);

just change the -2 to -1 and it works :)
 
Avast said:
^^ it works, but it kills the website in the link^^



the "i" is still there, but the www.mysite.com is disappeared :p


EDIT://

I found the mistake :)

the function have to be:
Code:
 function linkToViewer($link)
{
    $split = explode('/', $link);
    $img = $split[sizeof($split)-1];
    unset($split[sizeof($split)-1]);
unset($split[sizeof($split)-1]);
 
    return implode('/', $split).'/?v='.$img;
}


this part was wrong

Code:
unset($split[sizeof($split)-2]);

just change the -2 to -1 and it works :)

I updated my function.
 
Help! Script uploads the image, but does not give the result as there
MTkfc.jpg


Linkto page http://img.rebill.me/apiw.php
 
hello,

I will write a user desktop program.

What information should I?

Is it enough to just api url and api password?

I take any action required?
 
Sorry the digg but i wanted to try this out..

Since my site host dont allow direct php we need to host it in our other webhost. I think ive recreated (since the link is dead) the file correctly but then in the site is this:
assas.png



The buttons seem to work but when i choose a img and hit submit all i get is "File not found". All ive changed from the original code was the API key and the site url (pointing to the imagehost default homepage). What im i doing wrong?
 
Could someone please reupload this? It's very old, but I tried this on a different host but it only returns this code:

http://prntscr.com/1x9xdt

So the image uploaded from a different host uploads to the server but I would like it to return only the URL.
 
I'd also like to ask for a re-upload of this, I had to move my image host to a new server today and the fact that the documentation has disappeared in preparation for documentation that will eventually be written for the new version (which my friends are still trying to work out a lot of major problems on it).

Basically I got my image host up and running again but cURL just doesn't work and there is absolutely zero help that I have been able to find, I was hoping to string something together out of this maybe to help troubleshoot it :\
 
Sorry, this guide is deprecated. New API won't work in that way.
 
Well what about the existing Chevereto 2 users? Chevereto 3 has been nothing but a major headache for my friend and I'm definitely not interested in running an alpha stage script. I do look forward for Chevereto 3 when its ready, but for now I'd much prefer to continue using Chevereto 2 as this worked for me (until today when I moved my image host to a new data-center and lost ability to cURL upload via the publicly accessible API).
 
You can keep using it, deprecated means that you should not use it but is up to you. People even use version 1.9 so is up to you.
 
I understand what Deprecated is, and I kindly suggest that maybe you should first read up on how the word deprecated is used in the open source community before you yourself misuse the term.

Its very bad practice to refer to a version or build as being deprecated when superseding versions or builds are not production ready...
 
Deprecation means that something is recommended to not be used and the people who determinate this is the people to develop that software. Is that simple.

And by the way, "deprecation" is not a term owned by the Open Source community, is used in software development in general.

You are mixing two thing here, and that is that you are referring to the API has a missing feature in V3 where actually is not a function of V3 at all. Is normal that you remove certain functions from version to version because you need to either make huge changes or because that function is not longer needed. In our case API isn't there because V2 API has been reported to be an express gateway to DDoS attacks and other really nasty things. So instead of keep supporting that I'm in plan of remake it, but V3 introduced too much things to code the API in a weekend so you will have to wait.

Back to topic, you can keep using V2. Deprecate doesn't means obsolete.

Cheers.

P.S. I won't disclose how to DDoS or something V2 API.
 
Last edited:
Back
Top