0

Im creating a simple integration that should post our WooCommerce orders over to the Deep Data seciton via the API (V3)

Here is a simple example of the request Im trying to make.

Im running this script manually for the time being just to get it working. This is the array Im sending as my request using wp_remote_post($url, $request)

Array
(
[key] => KEY
[url] => URL/ecomOrders
[settings] => Array
    (
    [method] => POST
    [timeout] => 5
    [redirection] => 5
    [httpversion] => 1.0
    [user-agent] => WordPress/5.2.1; https://www.XXXX.com
    [blocking] => 1
    [body] => {"ecomOrder":{JSONORDER}}
    [headers] => Array
        (
            [Api-Token] => KEY
        )

    )

)

This is (part of) what I get back from my response.

[body] => 
[response] => Array
(
    [code] => 403
    [message] => Forbidden
)

I have double checked the API key and URL and just a side note, we are already using the same method and script details in a similar reques to add contacts which is working fine.

Here is the code Im using (all $var's are defined earlier in the script):

$request = array(
        'key' => $key,
        'url' => $url,
        'settings' => array(
            'method' => 'POST',
            'sslverify' => false,
            'timeout'     => 5,
            'redirection' => 0,
            'httpversion' => '1.0',
            'user-agent'  => 'WordPress/' . $wp_version . '; ' . home_url(),
            'blocking'    => true,
            'body'        => $body,
            'headers' => array(
                'Api-Token' => $key,
            )

        )
    );

    $response = wp_remote_post($url, $request);
Alex Knopp
  • 907
  • 1
  • 10
  • 30

1 Answers1

3

We just ran into a similar issue today; where all the headers and payload were set correctly, but the API was returning a 401.

Our payload needed to be sent as json and we had to explicitly define that in the headers. Like so:

'content-type' => 'application/json'   

Also, it look likes the request/args array isn't structured how WordPress recommends in the codex. (arguements)

$key = 'myKey';
$url = 'myURL'
$body = array('ecomOrder' => $myOrder);

$request = array(
    'method'      => 'POST',
    'sslverify'   => false,
    'timeout'     => 5,
    'redirection' => 0,
    'httpversion' => '1.0',
    'user-agent'  => 'WordPress/' . $wp_version . '; ' . home_url(),
    'blocking'    => true,
    'body'        => json_encode($body),
    'headers'     => array(
        'content-type' => 'application/json',
        'Api-Token'    => $key,
    )
);

$response = wp_remote_post($url, $request);

This may be a shot in the dark as I'm not familiar with Active Campaign's API, but hopefully it helps.

Resources

This stackoverflow article really helped.

Ryan
  • 510
  • 4
  • 16