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:
------------------------------------------------------------------------------------------------------------------------------------------------
Next is the PHP code.
We must check if the form was submitted
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.
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.
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.
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
or simply print it.
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
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