• Purchasing a license grants permission to use the software and to get Tech Support. A license purchase doesn't grant customization support.

How to use php CURL to interact with the API?

d1a8lo24

Network license
License owner
Joined
Jul 22, 2014
Messages
4
Points
51
Location
San Francisco
I am trying to use the API with PHP CURL but I haven't had any success. I have used some of the tutorials here but most of them are for V2.

I try to look for a tutorial for V3 but I didn't find any information in the forum.

Now I know the API works since I tried it by just doing a quick get with my browser and and the information provided the KEY and the URL to the image.

But with CURL I just get a 400 error.

So I will really appreciated if someone can write a quick tutorial on how to upload a local image that already exists on the server and send it to the API via post using CURL

And a second tutorial on how to the same but with the URL to the image.

Any help I will really appreciated.
 

Rodolfo

Chevereto Developer
Chevereto Staff
Joined
Oct 7, 2008
Messages
15,522
Points
237
Location
Chevereto HQ
Website
rodolfoberrios.com
cURL is just a wrapper for http requests which means that at the end is just a normal http request with fancy options and commands. So if you don't use the wrapper correctly is the same thing as trying to get an invalid url in your web browser.

At the end it will depend on where you want to apply the API. For instance if you want to connect to the API using a PHP script then you have several options aside from cURL and PHP cURL is not the same as command line cURL so the correct instructions depends on how you will use it. Also if you are trying to connect to an API running in https and you don't have the ssl extension then most likely the request will be denied.

cURL is the most robust and documented way to doing this and you can find documentation almost anywhere. For example: http://superuser.com/questions/149329/what-is-the-curl-command-line-syntax-to-do-a-post-request or here http://blog.derakkilgo.com/2009/06/07/send-a-file-via-post-with-curl-and-php/

Chevereto doesn't need any fancy thing or special rule or anything. Just use the wrapper correctly. FYI:

400 Bad Request
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.
 

d1a8lo24

Network license
License owner
Joined
Jul 22, 2014
Messages
4
Points
51
Location
San Francisco
Well here is my code which is the same one that I copied from here on a tutorial for V2 and also it looks the same as the one you pointed to.

PHP:
$target_url = 'http://www.mysite.com/api/1/upload/';
        //This needs to be the full path to the file you want to send.
        $file_name_with_full_path = realpath('./assets/images/logo.jpg');
        /* curl will accept an array here too.
        * Many examples I found showed a url-encoded string instead.
        * Take note that the 'key' in the array will be the key that shows up in the
        * $_FILES array of the accept script. and the at sign '@' is required before the
        * file name.
        */
        $post = array('key' => 'mykey','source'=>'@'.$file_name_with_full_path);
  
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$target_url);
        curl_setopt($ch, CURLOPT_POST,1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        $result=curl_exec ($ch);
        curl_close ($ch);
        echo $result;
In this case I don't get any type os response. and I try to see if the image has been uploaded and nothing.

I was able to do a quick get with no problems, the problem that I was having there was the bad formatting since I was passing them as parameters and the URL to images was being passed as URL encoded string so instead of http://www.site.com/image.jpg it was sending this http%3A%2F%2www.site.comF%2image.jpg

Now the post is the one I haven't been able to figure out so if thats how the code should look then I may be pointing to the wrong file. Or if you see anything wrong please let me know.

By the way thank you for such a great script, I am still trying to figure out a few but just out of the box it works perfect.