zaus / forms-3rdparty-xpost

Converts external submission from [Forms: 3rdparty Integration](http://wordpress.org/plugins/forms-3rdparty-integration/) plugin to XML post; optionally can add custom headers (to allow SOAP submissions).
4 stars 2 forks source link

Root elements question #15

Closed Carbie2000 closed 8 years ago

Carbie2000 commented 8 years ago

Hello!

So I am trying-out the plugin together with contact form 7 and it looks veryvery promising, exactly what I am looking for!! Unfortunately I got stuck..

Im trying to create a XML to be sent to a url looking like this --> http://www.xxxxx.xx/xxxx/query_xml?xml= followed by a xml that should be CGI/URLencoded.

The xml should be sent as below

<?xml version="1.0" encoding="UTF-8"?>
<shipment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <consignor>
    <id>hardcoded</id>
    <key>hardcoded</key>
    <currency>hardcoded</currency>
    <language>hardcoded</language>
    <encoding>UTF-8</encoding>
  </consignor>
  <parcels>
    <parcel>
      <weight>valuefromform</weight>
      <length>valuefromform</length>
      <width>valuefromform</width>
      <height>valuefromform</height>
    </parcel>
  </parcels>
  <address_from>
    <street_address_1>valuefromform</street_address_1>
    <street_address_2></street_address_2>
    <postal_code>valuefromform</postal_code>
    <city_name></city_name>
    <residential>hardcoded</residential>
    <country_code>hardcoded</country_code>
  </address_from>
</shipment>

What do you think is the best way of going about this?

I have been testing with setting the Root Element(s) to shipment/consignor, and this works, or the output is in the right position, but how do I set the next "group" - parcels?

Is there another way of setting this is up? Using the Post service format: URL instead? The output looks promising (pasted the hardcoded XML into the Root Elements(s)), but then Im not able to use the values from the form.

Any suggestion is greatly appreciated!

Regards

Carbie2000 commented 8 years ago

Hello,

Got it to work after some investigation on the forum and the XML is structured as it needs to be. But once its sent im getting an error in return since its not URLencoded, is it possible to configure so that the xml being sent is URLencoded before being sent?

Regards

Carbie2000 commented 8 years ago

Hello again,

Im stuck.. The problem is that the generated XML works if I just cut and past it into a browser and sending it to the api adress, but it doesnt work when sending using the form. The response gives an error (in swedish) translated to - "The call is not a properly formatted XML."

*** Service ***
Array
(
    [name] => Service 1
    [url] => http://api2.fraktjakt.se/fraktjakt/query_xml?xml=
    [forms] => Array
        (
            [0] => cf7_551
        )

    [success] =>
    [failure] =>
    [timeout] => 10
    [as-xpost] => true
    [xpost-wrapper] => ?xml version="1.0" encoding="UTF-8"?><shipment
    [xpost-header] =>
    [xpostac] => 1
    [mapping] => Array
        (
            [0] => Array
                (
                    [val] => 1
                    [lbl] => shipment (root)
                    [src] => http://www.w3.org/2001/XMLSchema-instance
                    [3rd] => @xmlns
                )

            [1] => Array
                (
                    [val] => 1
                    [lbl] => id
                    [src] => xxxx
                    [3rd] => consignor/id
                )

            [2] => Array
                (
                    [val] => 1
                    [lbl] => key
                    [src] => xxxx
                    [3rd] => consignor/key
                )

            [3] => Array
                (
                    [val] => 1
                    [lbl] => currency
                    [src] => SEK
                    [3rd] => consignor/currency
                )

            [4] => Array
                (
                    [val] => 1
                    [lbl] => language
                    [src] => eng
                    [3rd] => consignor/language
                )

            [5] => Array
                (
                    [val] => 1
                    [lbl] => encoding
                    [src] => UTF-8
                    [3rd] => consignor/encoding
                )

            [6] => Array
                (
                    [lbl] => weight
                    [src] => vikt
                    [3rd] => parcels/parcel/weight
                )

            [7] => Array
                (
                    [lbl] => length
                    [src] => langd
                    [3rd] => parcels/parcel/length
                )

            [8] => Array
                (
                    [lbl] => width
                    [src] => bredd
                    [3rd] => parcels/parcel/width
                )

            [9] => Array
                (
                    [lbl] => height
                    [src] => hojd
                    [3rd] => parcels/parcel/height
                )

            [10] => Array
                (
                    [lbl] => street_address_1
                    [src] => adress
                    [3rd] => address_from/street_address_1
                )

            [11] => Array
                (
                    [lbl] => postal_code
                    [src] => postnummer
                    [3rd] => address_from/postal_code
                )

            [12] => Array
                (
                    [val] => 1
                    [lbl] => residential
                    [src] => 1
                    [3rd] => address_from/residential
                )

            [13] => Array
                (
                    [val] => 1
                    [lbl] => country_code
                    [src] => SE
                    [3rd] => address_from/country_code
                )

        )

    [separator] => ,
)

*** Post (Form) ***
http://www.testsite.se/information/frakt/
Array
(
    [_wpcf7] => 551
    [_wpcf7_version] => 4.4
    [_wpcf7_locale] => sv_SE
    [_wpcf7_unit_tag] => wpcf7-f551-p157-o1
    [vikt] => 2
    [hojd] => 10
    [langd] => 20
    [bredd] => 15
    [adress] => test
    [postnummer] => 12345
    [_wpcf7_is_ajax_call] => 1
)

*** Post (to Service) ***
Array
(
    [timeout] => 10
    [body] => <?xml version="1.0" encoding="UTF-8"?>
<shipment xmlns="http://www.w3.org/2001/XMLSchema-instance"><consignor><id>xxxx</id><key>xxxx</key><currency>SEK</currency><language>eng</language><encoding>UTF-8</encoding></consignor><parcels><parcel><weight>2</weight><length>20</length><width>15</width><height>10</height></parcel></parcels><address_from><street_address_1>test</street_address_1><postal_code>12345</postal_code><residential>1</residential><country_code>SE</country_code></address_from></shipment>

    [headers] => Array
        (
            [Content-Type] => application/xml
        )

)

*** Response ***
Array
(
    [headers] => Array
        (
            [server] => nginx/0.8.54
            [date] => Wed, 02 Mar 2016 08:17:31 GMT
            [content-type] => text/xml; charset=utf-8
            [connection] => close
            [etag] => "74bfab3e61aec075c05fb9d8afffa9d0"
            [x-runtime] => 4
            [content-length] => 287
            [set-cookie] => _fraktjakt_session_id=fa38620987859e6955c0dab745d9f2d1; path=/; HttpOnly
            [cache-control] => private, max-age=0, must-revalidate
        )

    [body] => <?xml version='1.0' encoding='UTF-8'?><shipment xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><code>2</code><warning_message></warning_message><error_message>Anropet är inte en korrekt formaterat XML.</error_message><currency>SEK</currency><id /><shipping_products /></shipment>
    [response] => Array
        (
            [code] => 200
            [message] => OK
        )

    [cookies] => Array
        (
            [0] => WP_Http_Cookie Object
                (
                    [name] => _fraktjakt_session_id
                    [value] => fa38620987859e6955c0dab745d9f2d1

                    [expires] =>
                    [path] => /
                    [domain] => api2.fraktjakt.se
                    [httponly] =>
                )

        )

    [filename] =>
)

When taking the [body] info

<?xml version="1.0" encoding="UTF-8"?>
<shipment xmlns="http://www.w3.org/2001/XMLSchema-instance"><consignor><id>xxxx</id><key>xxxx</key><currency>SEK</currency><language>eng</language><encoding>UTF-8</encoding></consignor><parcels><parcel><weight>2</weight><length>20</length><width>15</width><height>10</height></parcel></parcels><address_from><street_address_1>test</street_address_1><postal_code>12345</postal_code><residential>1</residential><country_code>SE</country_code></address_from></shipment>

and pasting it into a browser http://api2.fraktjakt.se/fraktjakt/query_xml?xml=<?xml version="1.0" encoding="UTF-8"?><shipment xmlns="http://www.w3.org/2001/XMLSchema-instance"><consignor><id>xxxx</id><key>xxxxxx</key><currency>SEK</currency><language>eng</language><encoding>UTF-8</encoding></consignor><parcels><parcel><weight>2</weight><length>20</length><width>15</width><height>10</height></parcel></parcels><address_from><street_address_1

I get the the answer in the browser

<shipment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <code>0</code>
    <warning_message/>
    <error_message/>
    <currency>SEK</currency>
    <id>816212</id>
    <shipping_products>
        <shipping_product>
            <id>77</id>
            <name>UPS Express Saver</name>
            <description>UPS - UPS Express Saver</description>
            <arrival_time>1-2 dagar</arrival_time>
            <price>147.56</price>
            <tax_class>25.00</tax_class>
            <agent_info/>
            <agent_link/>
        </shipping_product>
        <shipping_product>
            <id>143</id>
            <name>DHL Home Delivery</name>
            <description>DHL Freight - DHL Home Delivery</description>
            <arrival_time>1-2 vardagar</arrival_time>
            <price>412.33</price>
            <tax_class>25.00</tax_class>
            <agent_info/>
            <agent_link/>
        </shipping_product>
    </shipping_products>
</shipment>

Any tips and tricks on what Im doing wrong?

Help is greatly appreciated!

Regards

zaus commented 8 years ago

I think the problem is that you need to submit the XML as a GET parameter, rather than it being the POST body (which is "normally" how you make XML webservice requests, and how this plugin works). You'll want to check out https://github.com/zaus/forms-3rdparty-integration#how-do-i-make-a-get-request-instead-of-post but you'll probably need something like:

add_filter('Forms3rdPartyIntegration_service_filter_args', 'f3p_send_as_get_param', 50, 3);
function f3p_send_as_get_param($post_args, $service, $form) {
    // rewrap the xml as a get parameter
    $post_args['body'] = array('query_xml'=>$post_args['body']);
    // do the bypass
    $post_args['response_bypass'] = wp_remote_get($service['url'], $post_args);
    // business as usual
    return $post_args;
}
zaus commented 8 years ago

Closing, no activity.