Woocommerce RestAPI not working properly 401 Error code - php

I've been trying to Get products details from my existing wordpress woocomerce website. I have created the Key and able to get data when use $woocommerce->get('') but when use $woocommerce->get('orders') or $woocommerce->get('products') it's giving 401 error.
Following is my code:
<?php
require __DIR__ . '/vendor/autoload.php';
use Automattic\WooCommerce\Client;
use Automattic\WooCommerce\HttpClient\HttpClientException;
$woocommerce = new Client(
'https://mycustomdomain.com',
'ck_XXXXXXXXXXXXXXXXXXXXXXXXXX',
'cs_XXXXXXXXXXXXXXXXXXXXXXXXXXX',
[
'wp_api' => true,
'version' => 'wc/v3',
'query_string_auth' => true,
]
);
$results = $woocommerce->get('orders');
//$results = $woocommerce->get('');
echo "<pre>";
print_r($results);
echo "</pre>";
I have also uninstalled all the plugins and upgraded the PHP version to the latest one and also, increased the memory which is now 256m but nothing works properly.

//maybe if you change this:
[
'wp_api' => true,
'version' => 'wc/v3',
'query_string_auth' => true,
]
// enter code here
to this:
[
'wp_api' => false,
'version' => 'wc/v3',
'query_string_auth' => false,
]

Related

Updating a JSON file with Wordpress REST API

I am trying to insert a new object in a JSON file that is placed in my WP theme root, by using wp_remote_post command. The page does not throw any visible errors, but the JSON file is not getting updated either. I prefer this method instead of file_put_contents function so please help me understand this approach. Thanks.
front-page.php:
if(isset($_POST['save']) && !empty($_POST['specie_name']) && !empty($_POST['specie_rate'])):
$specie_name= $_POST['specie_name'];
$specie_rate=(int)$_POST['specie_rate'];
$url = get_template_directory_uri() . '/species.json';
$body = wp_json_encode(array(
'specie' => $specie_name,
'rate' => $specie_rate,
));
$args = array(
'body' => $body,
'headers' => [ 'Content-Type' => 'application/json', ],
'timeout' => 60,
'redirection' => 5,
'blocking' => true,
'httpversion' => '1.0',
'sslverify' => false,
'data_format' => 'body',
);
wp_remote_post( $url, $args );
endif;
The JSON looks like this:
[
{
"specie": "Oscar",
"rate": 300
},
{
"specie": "Piranha",
"rate": 400
},
]
I am closing this question as I believe I have figured the answer. That is, to get the json contents with wp_remote_get, and then using file_put_contents to push in data.

Laravel Dropbox API v2 - Empty File on Upload

What I have till now
Right now I have a working oauth2 authentication between a laravel user and the dropbox API. Every user can upload files to their personal folder.
The Problem
After Uploading a file with laravel with the Dropbox API v2 I can see that there is a empty (0 Bytes) file uploaded.
Used to accomplish this task:
Laravel
Guzzle
Dropbox API Library
What am I missing?
The Code
My function for processing a form looks like this:
$formFile = $request->file('fileToUpload');
$path = $formFile->getClientOriginalName();
$file = $formFile->getPathName();
$result = Dropbox::files()->upload($path, $file);
return redirect('dropboxfiles');
And my files->upload function in my Dropbox Library looks like this:
$client = new Client;
$response = $client->post("https://content.dropboxapi.com/2/files/upload", [
'headers' => [
'Authorization' => 'Bearer '.$this->getAccessToken(),
'Content-Type' => 'application/octet-stream',
'Dropbox-API-Arg' => json_encode([
'path' => $path,
'mode' => 'add',
'autorename' => true,
'mute' => true,
'strict_conflict' => false
])
],
'data-binary' => '#'.$file
]);
The file, as I said, gets uploaded successfully. Correct name, but 0 Bytes. So empty file.
Thank you so much in advance for your help!
Update
With the following code I made it work. My question is though if there is a better "Laravel-Like" Solution instead of using fopen?
$response = $client->post("https://content.dropboxapi.com/2/files/upload", [
'headers' => [
'Authorization' => 'Bearer '.$this->getAccessToken(),
'Dropbox-API-Arg' => json_encode([
'path' => $path,
'mode' => 'add',
'autorename' => true,
'mute' => true,
'strict_conflict' => false
]),
'Content-Type' => 'application/octet-stream',
],
'body' => fopen($file, "r"),
]);
How #Greg mentioned (see cross-linking reference) I was able to solve this issue by using
'body' => fopen($file, "r"),
instead of
'data-binary' => '#'.$file
This is, how Greg mentioned, because data-binary is used in Curl requests. Other HTTP Clients, like Guzzle in my case use different names.

Create and publish products with ebay API's

i need create and publish products in ebay with php , i find this sdk, but this not work for me, some have an code or example for this?
https://github.com/davidtsadler/ebay-sdk-php
in this sdk i get this error
Cannot read credentials from /.ebay_sdk/credentials in C:\wamp64\www\ebay\vendor\dts\ebay-sdk-php\src\Credentials\CredentialsProvider.php on line 132
but i changue the credentials in CredentialsProvider.php
I am using the same SDK (and it's amazing how awesome it is). The guy who created this SDK also provides some examples. You can find them all here. In the Trading folder, you will find 6 (I think) different examples of how to list an item using this SDK.
If you still have problems with the credentials, you could add the credentials manually (but I don't recommend it). Instead of using
$service = new Services\TradingService([
'credentials' => $config['sandbox']['credentials'],
'sandbox' => true,
'siteId' => $siteId
]);
you could use
$service = new Services\TradingService([
'credentials' => [
'devId' => 'YOUR_DEV_ID',
'appId' => 'YOUR_APP_ID',
'certId' => 'YOUR_CERT_ID',
],
'siteId' => EBAY_SITE_ID,
'sandbox' => true, // or false, up to you
'token' => 'YOUR_TOKEN'
]);

Encrypted Cookies in Slim Framework v3

I am writing and application in Slim Framework v3.1. I am a lot confused on how to correctly set and get the cookies using Slim's methods.
I need your help in understanding what is the right way to read and write cookies with encryption enabled.
I also need to know how to enable encryption and decryption for the same.
Currently my $app is initialised this way -
$settings = require __DIR__ . '/../src/settings.php';
$app = new \Slim\App($settings);
My settings.php looks like below -
return [
'settings' => [
'displayErrorDetails' => true, // set to false in production
'addContentLengthHeader' => false, // Allow the web server to send the content-length header
// Renderer settings
'renderer' => [
'template_path' => __DIR__ . '/../templates/',
],
// Cookies Encryption
'cookies.encrypt' => true,
'cookies.secret_key' => '53cr3t',
'cookies.cipher' => OPENSSL_CIPHER_AES_256_CBC,
'cookies.cipher_mode' => MCRYPT_MODE_CBC,
],
];

Slim 3 - Error is not shown

I am trying to use zeuxisoo/slim-whoops to show errors, but for some reason I am only getting white screen without any message show. This is the code I am using (I don't know if it does matter, I am using quick PHP host (php -S localhost:8000):
require __DIR__ . '/../vendor/autoload.php';
$app = new Slim\App([
'settings' => [
'displayErrorDetails' => true,
'debug' => true,
'whoops.editor' => 'sublime',
]
]);
$app->add(new \Zeuxisoo\Whoops\Provider\Slim\WhoopsMiddleware);
Make sure that your file begins with <?php and that after your $app->add(...); you call $app->run();:
<?php
require __DIR__ . '/../vendor/autoload.php';
$app = new Slim\App([
'settings' => [
'displayErrorDetails' => true,
'debug' => true,
'whoops.editor' => 'sublime',
]
]);
$app->add(new \Zeuxisoo\Whoops\Provider\Slim\WhoopsMiddleware);
$app->run();

Categories