Page 1 of 1

Webhooks/Requests

Posted: Tue Nov 24, 2020 9:21 am
by RytisT
Hi. I'm trying to pass file with data from WordPress, WooCommerce orders.

I'm only very little familiar with webhooks, not sure how I'm supposed to do this.

Should this be like this ->

WooCommerce passed file path + order data through webhook to script in linux server, that same script passes same info to switch webhook to the path I entered in webhook element?

(Is that how webhook in switch supposted to work, I enter custom path, let's say "test" and url would be www.ip+port/test ?)

Thanks for any help.

Re: Webhooks/Requests

Posted: Tue Nov 24, 2020 9:21 am
by jan_suhr
First you have to add some code to your webshop so that it will send out the call to your Switch webhook. In that call you will send a payload that contains information about your order. In this stage the job file stays on your webserver and is waiting in a temporary folder.

When the webhook arrives to your Switch server the payload is read and becomes Metadata for the flow to use. Next step will be a HTTP Request from Switch to pickup and download the job file to your flow. The Metadata is added to the job and then it goes on in the flow.

Most of the work for you is on the PHP in your webshop that needs to be added. If the webshop can save an XML about the order that might be easier. Then you pickup that XML-file too with the HTTP Request. Not so much to change in the webshop code with that solution.

Re: Webhooks/Requests

Posted: Tue Nov 24, 2020 2:45 pm
by RytisT
jan_suhr wrote:
Tue Nov 24, 2020 9:21 am
First you have to add some code to your webshop so that it will send out the call to your Switch webhook. In that call you will send a payload that contains information about your order. In this stage the job file stays on your webserver and is waiting in a temporary folder.

When the webhook arrives to your Switch server the payload is read and becomes Metadata for the flow to use. Next step will be a HTTP Request from Switch to pickup and download the job file to your flow. The Metadata is added to the job and then it goes on in the flow.

Most of the work for you is on the PHP in your webshop that needs to be added. If the webshop can save an XML about the order that might be easier. Then you pickup that XML-file too with the HTTP Request. Not so much to change in the webshop code with that solution.
Thanks. I did what you said, I made so it just saves an XML, however it saves quite a messy PDF, I don't imagine Switch would read this? http://prntscr.com/vp5qzi

This is what I got for the code:

Code: Select all

function array2xml($array, $xml = false){

if($xml === false){
    $xml = new SimpleXMLElement('<result/>');
}

foreach($array as $key => $value){
    if(is_array($value)){
        array2xml($value, $xml->addChild($key));
    } else {
        $xml->addChild($key, $value);
    }
}

return $xml->asXML();
}

$raw_data = file_get_contents('php://input');
$jSON = json_decode($raw_data, true);
$id = $jSON["id"].'.xml';
$xml = array2xml($jSON, false);

echo '<pre>';
$output = print_r($xml, true);
file_put_contents($id, $output);

Re: Webhooks/Requests

Posted: Tue Nov 24, 2020 3:02 pm
by jan_suhr
As long as the XML is valid it can look really bad.

Re: Webhooks/Requests

Posted: Tue Nov 24, 2020 4:07 pm
by jan_suhr
You can also add my app Webhook warning. It will catch errors on the webserver ( you need to code it to do that) and send them to a Webhook on your Switch server and then display the error message from your webshop in the Switch messages.

https://www.enfocus.com/en/appstore/pro ... ok-warning

Re: Webhooks/Requests

Posted: Mon Nov 30, 2020 7:21 am
by RytisT
jan_suhr wrote:
Tue Nov 24, 2020 3:02 pm
As long as the XML is valid it can look really bad.
Okay, so I got XML that I'm not sure if it's valid, but does sure looks just like a normal XML. (Except that it's messy: http://prntscr.com/vsm5n5 )

Anyways, so now I need to pickup this XML in switch, so that switch can read one of metadata lines and download file from URL.
How do I exactly point Switch to read certain line?

Thanks.

Re: Webhooks/Requests

Posted: Mon Nov 30, 2020 8:40 am
by jan_suhr
You use the XML Pickup tool in the Metadata section. The in the properties you assign a variable to use the value from one XML node.

I assume you have the Metadata Module.

Re: Webhooks/Requests

Posted: Mon Nov 30, 2020 9:04 am
by freddyp
Anyways, so now I need to pickup this XML in switch, so that switch can read one of metadata lines and download file from URL.
How do I exactly point Switch to read certain line?
Once your XML is attached as a dataset (in this case attached to itself) you can use what is called an XPath in the Metadata section of the Switch variables pane. You would use it in a single-line text with variables on the URL property of "HTTP request" in order to download the file.

XPaths use a certain syntax for finding information in an XML. The correct XPath depends on the structure of the XML and of course on what information you want to extract from it. With only a screenshot and a vague idea of what it is that you are looking for in the XML it is not possible to give you any further advice.

By the way, the only reason your XML looks messy is because it a screenshot of the non-formatted contents. If you opened this in a real XML editor, you would be able to see the tree structure of the file and that will probably make things a lot clearer.