-2

Problem/Motivation php://input always returns NULL

Steps to reproduce I have been struggling with this for weeks now. i am completely newbie and trying to configure a webhook to catch the Payment_intent.succeeded event from stripe and do some actions accordingly... the probelm is, i always get the below error :

TypeError: array_keys(): Argument #1 ($array) must be of type array, null given in array_keys() (line 279 of /var/www/html/vendor/stripe/stripe-php/lib/StripeObject.php).

I narrowed down the issue to: always getting null from 'php://input' stream, i tried almost every trick i found online in previous posts but none of them worked for me..

I appreciate any support here..

Here is my Code this is written in a file called webhook.php in custom_module directory. and is called in custom_module.module using require 'webhook.php'

<?php

require_once '../vendor/autoload.php';
require_once 'secrets.php';

\Stripe\Stripe::setApiKey($stripeSecretKey);
// Replace this endpoint secret with your endpoint's unique secret
// If you are testing with the CLI, find the secret by running 'stripe listen'
// If you are using an endpoint defined with the API or dashboard, look in your webhook settings
// at https://dashboard.stripe.com/webhooks
$endpoint_secret = 'whsec_....';

$payload = @file_get_contents('php://input');
$event = null;

var_dump($payload);

try {
   $event = \Stripe\Event::constructFrom(
   json_decode($payload, true)
   );
}
catch(\UnexpectedValueException $e) {
   // Invalid payload
   echo '⚠️ Webhook error while parsing basic request.';
   http_response_code(400);
   exit();
}

I was expecting to recieve the value of the payload but it always shows null.

dawa
  • 1
  • 1
    Does this answer your question? [php://input is null though url parameters are sent](https://stackoverflow.com/questions/48018862/php-input-is-null-though-url-parameters-are-sent) – Wahyu Kristianto Mar 11 '23 at 13:07

1 Answers1

-2

As documentation says:

php://input is not available with enctype="multipart/form-data".

I think you are using it to read a form with enctype="multipart/form-data".

Consider using php://stdin instead.

Alternative

Although something unusual can happen.

The request body data has been read, but its contents were not stored anywhere. php://input cannot give us anything.

esqew
  • 42,425
  • 27
  • 92
  • 132
Alijvhr
  • 1,695
  • 1
  • 3
  • 22
  • tried that, still getting null.. – dawa Mar 12 '23 at 13:02
  • Sir I think You're getting everything wrong! who is calling your `custom_module.module` file? I think this is a simple get and there is no json in body at all! – Alijvhr Mar 13 '23 at 06:40
  • 1
    i think i kinda understand what you mean. is it because the whole thing is just a get not a post? how can i fix that. do you know of a link you can share of how i can structure it and validate during development?? Thanks – dawa Mar 14 '23 at 10:28
  • Just use `$_GET` variable. Data is in url not in body – Alijvhr Mar 14 '23 at 11:58