phpandroidsha1

Calling Pho.to API


I am trying to call the Pho.to API to edit photos, and every time I try to POST I get the same error. I've double and triple checked my app_id and my key, and I can't figure out what I'm doing wrong. I'm currently using the ARC Chrome Extension to call this API, so I haven't even started coding that part yet, I'm just trying to get a real response back from the API to make sure it even works.

I followed the instructions in their docs as well as I could. Here's the link for reference: http://developers.pho.to/documentation/sending-requests

Here is my API call:

http://opeapi.ws.pho.to/addtask?APP_ID=<my-app-id>&KEY=<my-key>&SIGN_DATA=910ceb5bdb238b9248a34cce8b29ba64d5f239df

And here is the response I get back (don't be deceived by the 200):

Status: 200 OK

<?xml version="1.0" ?>
<image_process_response>
    <status>SecurityError</status>
    <err_code>614</err_code>
    <description>Error in POST parameters: one or more parameters (DATA , SIGN_DATA or APP_ID) are empty</description>
</image_process_response>

Here's the PHP code I use to create the SHA1 for SIGN_DATA:

<?php
    echo hash_hmac('SHA1', '<image_process_call><lang>en</lang><image_url order="1">http://www.w3schools.com/html/pic_mountain.jpg</image_url><methods_list><method order="1"><name>desaturation</name></method><method order="2"><name>cartoon</name><params>fill_solid_color=1;target_color=(255,255,255);border_strength=20;border_width=3</params></method></methods_list><result_format>png</result_format><result_size>600</result_size></image_process_call>','<my-key>');
?>

Here is the XML from above, formatted for readability:

<image_process_call>
    <lang>en</lang>
    <image_url order="1">http://www.w3schools.com/html/pic_mountain.jpg</image_url>
    <methods_list>
        <method order="1">
            <name>desaturation</name>
        </method>
        <method order="2">
            <name>cartoon</name>
            <params>fill_solid_color=1;target_color=(255,255,255);border_strength=20;border_width=3</params>
        </method>
    </methods_list>
    <result_format>png</result_format>
    <result_size>600</result_size>
</image_process_call>

Any help would be appreciated.


Solution

  • So I figured out what was wrong. Here is my detailed solution for anyone else that may run into similar problems with this api (regardless of platform):

    Part of the problem (as @u_mulder pointed out) is that DATA needs to be sent up along with SIGNED_DATA so that the SHA1 can be decoded on the other end.

    The other piece that fixed my problem was removing <lang>en</lang>. For whatever reason, that was returning Error 613: Invalid SIGN_DATA parameter. English is the default language anyway, so it was unnecessary.

    So after fixing those things, here's my final url:

    http://opeapi.ws.pho.to/addtask/?app_id=<my-app-id>&key=<my-key>9&sign_data=e456c393d11797c1a2945a85dd49ba2208cc66de&data=%3Cimage_process_call%3E%3Cimage_url+order%3D%221%22%3Ehttp%3A%2F%2Fwww.heroesandheartbreakers.com%2Fimages%2Fstories%2Fblogarticles%2F2016%2FJanuary2016%2FTV-Recap-Arrow-4x11-Olicity-is-home-470.jpg%3C%2Fimage_url%3E%3Cmethods_list%3E%3Cmethod+order%3D%221%22%3E%3Cname%3Ecartoon%3C%2Fname%3E%3Cparams%3Efill_solid_color%3D1%3Btarget_color%3D%28255%2C255%2C255%29%3Bborder_strength%3D20%3Bborder_width%3D1%3C%2Fparams%3E%3C%2Fmethod%3E%3C%2Fmethods_list%3E%3Cresult_format%3Epng%3C%2Fresult_format%3E%3Cresult_size%3E1500%3C%2Fresult_size%3E%3C%2Fimage_process_call%3E
    

    Notice that the url is encoded. This may or may not be necessary, I just encoded it to be safe.

    This returns:

    <?xml version="1.0" ?>
    <image_process_response>
        <request_id>010afc13-6bba-44dd-b278-4f3bd1e41946</request_id>
        <status>OK</status>
        <description />
        <err_code>0</err_code>
    </image_process_response>
    

    And I can now use request_id to get the url of the edited image:

    http://opeapi.ws.pho.to/getresult?request_id=010afc13-6bba-44dd-b278-4f3bd1e41946
    

    Which returns the following xml:

    <image_process_response>
        <request_id>010afc13-6bba-44dd-b278-4f3bd1e41946</request_id>
        <status>OK</status>
        <result_url>http://worker-images.ws.pho.to/i1/3BCB160A-691A-458B-9161-67AFA8A9EAA0.png</result_url>
        <result_url_alt>http://worker-images.ws.pho.to.s3.amazonaws.com/i1/3BCB160A-691A-458B-9161-67AFA8A9EAA0.png</result_url_alt>
        <nowm_image_url>http://worker-images.ws.pho.to/i1/3BCB160A-691A-458B-9161-67AFA8A9EAA0.png</nowm_image_url>
    </image_process_response>
    

    So the url of the final edited image is http://worker-images.ws.pho.to/i1/3BCB160A-691A-458B-9161-67AFA8A9EAA0.png (I believe links expire after 24 hours)

    And we're done!

    If you'd like to check out how I implemented this api in a simple Android app, here's the github link: https://github.com/youravgjoe/ColoringPageGenerator

    Before:

    Before

    After:

    After