Hope somebody can help me with this issue.
Issue:
I have a woocommerce webhook that fires off when a product gets updated and sends data to a script to store details about the product in a separate database.
When I update a product and check the woocommerce log I can see that the webhook fires and the payload gets delivered correctly, including the body with all the details, such as 'id', 'store_id', 'name' and so on. However, the response shows a 500 Internal Server error, saying:
Undefined array key "store_id" in file "ProductController.php" on line 40
I'm using WooCommerce 8.0.2 and WordPress 6.3.1
Code:
Here's part of the code that handles the payload and stores the data. I'm using CodeIgniter 4.
ProductController.php
use App\Controllers\BaseController;
use App\Models\WooCommerce\ProductModel;
use CodeIgniter\API\ResponseTrait;
class ProductController extends BaseController{
use ResponseTrait;
public function store() {
//get webhook payload
$data = $this->request->getPost();
//populate product data from webhook payload
$productData = [
'store_id' => $data['store_id'], // this is line 40
'product_id' => $data['product_id']
];
$product = new ProductModel();
$product=>save($productData);
}
}
What I've done so far:
I've tested the webhook payload using Pipedream.com, and I know that the body gets sent and is not empty. The array key "store_id" is right there and has a value.
So where am I losing the data?
I did some research and came about this question form-post-empty-in-controller-from-view. The accepted answer says that it could be a .htaccess issue with RewriteRule according to this CI github post issue https://github.com/bcit-ci/CodeIgniter/issues/242
I'm not very familiar with .htaccess and its RewriteRules, and don't know if that's even causing the issue.
If somebody could have a look at my .htaccess file and see if there's anything that might be causing it, that would be fantastic. Or is there anything else that I should be looking into?
.htaccess:
# Disable directory browsing
Options -Indexes
# ----------------------------------------------------------------------
# Rewrite engine
# ----------------------------------------------------------------------
# Turning on the rewrite engine is necessary for the following rules and features.
# FollowSymLinks must be enabled for this to work.
<IfModule mod_rewrite.c>
Options +SymLinksIfOwnerMatch
RewriteEngine On
# If you installed CodeIgniter in a subfolder, you will need to
# change the following line to match the subfolder you need.
# http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
# RewriteBase /
# Redirect Trailing Slashes...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Rewrite "www.example.com -> example.com"
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to the front controller, index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\s\S]*)$ index.php?/$1 [L,NC,QSA]
# Ensure Authorization header is passed along
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
ErrorDocument 404 index.php
</IfModule>
# Disable server signature start
ServerSignature Off
# Disable server signature end
Edit:
Here are two screenshots that show that the webhook got delivered. One is from the woocommerce log, and the other when I sent it to pipedream for testing.
Thanks to CBroe's comment I got it working.
Here’s how:
The webhook is sending the data as JSON, so CI’s $this->request->getPost()
won’t populate $_POST. Therefore I got the error message Undefined array key "store_id"
.
In order to get the data I need to get the content from php://input
and parse it myself.
Turns out CI already has a dedicated way to do that: getJSON()
.
By default, this will return any objects in the JSON data as objects. Passing in true
as the first parameter converts it to associative arrays.
So changing $this->request->getPost()
to $this->request->getJSON(true)
in ProductController.php fixed it.