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);
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.
Related
I want to backup all Facebook photos from a user with a webapp made in PHP/Laravel. What I do right now is:
I first get all albums with /me/albums
Then I get all photos from each album with /<album id>/photos
And then I retrieve each photo with /<photo id>/picture?type=normal
And that gets me this:
Facebook\FacebookResponse::__set_state(array(
'httpStatusCode' => 302,
'headers' =>
array (
'x-app-usage' => '{"call_count":0,"total_cputime":0,"total_time":0}',
'location' => 'https://scontent.fros6-1.fna.fbcdn.net/v/t31.0-8/s720x720/241688_10150189954577077_6076941_o.jpg?_nc_cat=106&ccb=2&_nc_sid=e007fa&_nc_ohc=6BG0bnvmDO0AX-ayiLu&_nc_ht=scontent.fros6-1.fna&tp=7&oh=2eb322e48d2bcba4c4f06b95d8803922&oe=5FE0AF19',
'expires' => 'Sat, 01 Jan 2000 00:00:00 GMT',
'x-fb-request-id' => 'AV2g1mnIDjWDzIgHyD48oNx',
'strict-transport-security' => 'max-age=15552000; preload',
'x-fb-trace-id' => 'E5eCApEMOPW',
'facebook-api-version' => 'v9.0',
'content-type' => 'image/jpeg',
'x-fb-rev' => '1003022644',
'cache-control' => 'private, no-cache, no-store, must-revalidate',
'vary' => 'Accept-Encoding',
'pragma' => 'no-cache',
'access-control-allow-origin' => '*',
'x-fb-debug' => '9brMqjQf3QpZ95/RjANHrhZvYY30VzEivA91+bsysa+SwAib5U6gZl5jlqjOscOusDviJe0wjoCuIJoyc67bbQ==',
'content-length' => '0',
'date' => 'Sun, 22 Nov 2020 17:22:05 GMT',
'alt-svc' => 'h3-29=":443"; ma=3600,h3-27=":443"; ma=3600',
),
'body' => '',
'decodedBody' =>
array (
),
'request' =>
Facebook\FacebookRequest::__set_state(array(
'app' =>
Facebook\FacebookApp::__set_state(array(
'id' => '<hidden>',
'secret' => '<hidden>',
)),
'accessToken' => <hidden>,
'method' => 'GET',
'endpoint' => '/10150189954577077/picture?type=normal',
'headers' =>
array (
'Content-Type' => 'application/x-www-form-urlencoded',
),
'params' =>
array (
),
'files' =>
array (
),
'eTag' => NULL,
'graphVersion' => 'v9.0',
)),
'thrownException' => NULL,
))
So what I need from there is the location. I store that object in $response. But I can't do $response['headers'] because it says is not an array (using json_encode gives and empty array) and also can't do $response->headers or $response->headers() because it says it's protected.
Who do I access image location within that Facebook Response?
The PHP SDK apparently does not have a dedicated section in the FB docs any more, they are redirecting directly to GitHub.
If you check the /docs/reference folder there and look for the class in question, you can find the available public methods listed there though.
https://github.com/facebookarchive/php-graph-sdk/blob/master/docs/reference/FacebookResponse.md
getHeaders()
public array getHeaders()
Returns the response headers that were returned.
I would like to connect one Razorpay Payment API with WordPress, API has authentication token using username and password.
is there any builtin functionality available in WordPress to make a call and handle response?
you can use wp_remote_get()
for example
wp_remote_get( 'http://www.example.com/index.php?action=foo', array( 'timeout' => 120, 'httpversion' => '1.1' ) );
you can also control all request parameters like headers and body data.
Default Usage
global $wp_version;
$args = array(
'timeout' => 5,
'redirection' => 5,
'httpversion' => '1.0',
'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url(),
'blocking' => true,
'headers' => array(),
'cookies' => array(),
'body' => null,
'compress' => false,
'decompress' => true,
'sslverify' => true,
'stream' => false,
'filename' => null
);
Reference : More info
I'm in the process of convert from cURL to Guzzle, and got most of it working.
GET requests working great etc.
My problem is the POST request, getting Schema validation errors.
It works in curl, so I'm a bit confused... well, a lot.
Client error: `POST https://restapi.e-conomic.com/customers` resulted in a `400 Bad Request` response:
{"message":"Schema validation failed.","errorCode":"E00500"
I hope one of you can tell me, if I did something wrong in the convert? Maybe my arrays needs to be formatted in another way.
This is my old working "cURL code":
$data = array(
'name' => 'Test User',
'address' => 'Road 123',
'email' => 'morten#domain.com',
'zip' => '9000',
'city' => 'City',
'country' => 'Danmark',
'corporateIdentificationNumber' => '12345678',
'customerGroup' => array(
'customerGroupNumber' => 1
),
'currency' => 'DKK',
'paymentTerms' => array(
'paymentTermsNumber' => 1
),
'vatZone' => array(
'vatZoneNumber' => 1
)
);
$options = array(
CURLOPT_URL => 'https://restapi.e-conomic.com/customers',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => array(
'X-AppSecretToken:[removed]',
'X-AgreementGrantToken:[removed]',
'Content-Type:application/json; charset=utf-8'
),
CURLOPT_POSTFIELDS => json_encode($data)
);
curl_setopt_array($ch, $options);
This is my new "guzzle code", that is causing me problems:
$client = new GuzzleHttp\Client();
$headers = [
'X-AppSecretToken' => '[removed]',
'X-AgreementGrantToken' => '[removed]',
'Content-Type' => 'application/json;charset=utf-8',
'debug' => false
];
$form_params = [
'name' => 'Test User',
'address' => 'Road 123',
'email' => 'test#email.dk',
'zip' => '9000',
'city' => 'City',
'country' => 'Danmark',
'corporateIdentificationNumber' => '12345678',
'customerGroup' => [
'customerGroupNumber' => 1
],
'currency' => 'DKK',
'paymentTerms' => [
'paymentTermsNumber' => 1
],
'vatZone' => [
'vatZoneNumber' => 1
]
];
$response = $client->post('https://restapi.e-conomic.com/customers', [
'headers' => $headers,
'form_params' => $form_params
]);
I tried to use the "body" parameter, as stated in the Guzzle documentation, but received this error:
Passing in the "body" request option as an array to send a POST request has been deprecated. Please use the "form_params" request option to send a application/x-www-form-urlencoded request, or the "multipart" request option to send a multipart/form-data request.
I'm not sure what to do and really hope, that one of you guys will tell me what i'm doing wrong.
https://restapi.e-conomic.com/schema/customers.post.schema.json#_ga=2.167601086.1488491524.1500877149-796726383.1499933074
https://restdocs.e-conomic.com/#post-customers
I had to post the quest as json:
$response = $client->post('https://restapi.e-conomic.com/customers', [
'headers' => $headers,
'json' => $form_params
]);
I'm making small steps into this project I am working on. Now creating and registering a webhook. I'm getting the below response:
400 - Invalid Header
I have tried the following code:
// Send a request to register a web hook
$http2 = new Client('https://api.bigcommerce.com', array(
'request.options' => array(
'exceptions' => false,
'headers' => array(
'X-Auth-Client' => $client_id,
'X-Auth-Token' => $access_token,
'Content-Type' => 'application/json',
'X-Custom-Auth-Header' => $access_token,
)
)
));
$request = $http2->post('/'.$store_hash.'/v2/hooks', null, array(
'scope' => 'store/order/*',
'destination' => 'https://example.com/process_order.php',
'is_active' => true
));
$response = $request->send();
$body = $response->getBody(true);
var_dump($body);
echo '<p>Status Code: ' . $response->getStatusCode() . '</p>';
... and
// Send a request to register a web hook
$http2 = new Client('https://api.bigcommerce.com', array(
'request.options' => array(
'exceptions' => false,
'headers' => array(
'X-Auth-Client' => $client_id,
'X-Auth-Token' => $access_token,
'Content-Type' => 'application/json',
)
)
));
$request = $http2->post('/'.$store_hash.'/v2/hooks', null, array(
'scope' => 'store/order/*',
'headers' => array(
'X-Custom-Auth-Header' => $access_token,
),
'destination' => 'https://example.com/process_order.php',
'is_active' => true
));
$response = $request->send();
$body = $response->getBody(true);
var_dump($body);
echo '<p>Status Code: ' . $response->getStatusCode() . '</p>';
I am working with the documentation here:
https://developer.bigcommerce.com/api/stores/v2/webhooks#create-a-hook
However, I can't seem to work out what {secret_auth_password} is as well? The documentation doesn't explain this. I am sending the Client ID and Client Header as part of the headers as well.
Still getting Invalid Header as a response.
I am using Guzzle.
Can anyone assist me on this please?
I have finally worked out what I did wrong after numerous attempts.
Answer: send the data in JSON format.
Resolved Code:
// Send a request to register a web hook
$http3 = new Client('https://api.bigcommerce.com', array(
'request.options' => array(
'exceptions' => false,
'headers' => array(
'X-Auth-Client' => $client_id,
'X-Auth-Token' => $access_token,
'Content-Type' => 'application/json',
'Accept' => 'application/json',
)
)
));
$request = $http3->post('/'.$store_hash.'/v2/hooks', null, json_encode(array(
'scope' => 'store/order/statusUpdated',
'destination' => 'https://example.com/process_order.php',
)));
$response = $request->send();
I'm Trying to correctly use wp_remote_get in my wordpress plugin which uses our API. The API checks the request body for is_json, which returns 4 - aka JSON_ERROR_SYNTAX. Using cURL has the desired response. Here's my code:
$args = array(
'body' => array('api_key' => 123),
'timeout' => '5',
'httpversion' => '1.0',
'blocking' => true,
'headers' => array(),
'cookies' => array()
);
$result = wp_remote_request('https://myapi.endpoint/api/1.0/request', $args);
var_dump($result['body'])
var_dump: string(13) ""api_key=123""
whereas the var_dump of my cURL request is string(15) "{"api_key=123"}"
Any ideas as to what I am doing incorrectly?
As Marc B stated, wp isn't sending json. You need to set the content type in the headers, and also send it as json:
$body = array('api_key' => 123);
$headers = array (
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Content-Length' => strlen(json_encode($body)
);
$args = array(
'method' => 'POST',
'headers' => $headers,
'body' => json_encode($body)
);