Webhooks/Requests

Post Reply
RytisT
Member
Posts: 135
Joined: Fri Aug 17, 2018 3:18 pm

Webhooks/Requests

Post 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.
jan_suhr
Advanced member
Posts: 586
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: Webhooks/Requests

Post 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.
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps
RytisT
Member
Posts: 135
Joined: Fri Aug 17, 2018 3:18 pm

Re: Webhooks/Requests

Post 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);
jan_suhr
Advanced member
Posts: 586
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: Webhooks/Requests

Post by jan_suhr »

As long as the XML is valid it can look really bad.
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps
jan_suhr
Advanced member
Posts: 586
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: Webhooks/Requests

Post 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
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps
RytisT
Member
Posts: 135
Joined: Fri Aug 17, 2018 3:18 pm

Re: Webhooks/Requests

Post 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.
jan_suhr
Advanced member
Posts: 586
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: Webhooks/Requests

Post 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.
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps
freddyp
Advanced member
Posts: 1008
Joined: Thu Feb 09, 2012 3:53 pm

Re: Webhooks/Requests

Post 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.
Post Reply