PHP字符串作为数组参数传递

huangapple go评论81阅读模式
英文:

PHP String is passed as Array param

问题

It looks like you're dealing with HTML-encoded content in your PHP code, and you're encountering issues with the $respuesta variable. The error message indicates that unlockAndPushBotMessage expects a string as its first argument, but it's receiving an array.

To fix this issue, you should decode the HTML-encoded content in the $respuesta variable before passing it to the unlockAndPushBotMessage function. You can use the html_entity_decode function in PHP for this purpose. Here's how you can modify your code:

Replace this line:

  1. echo gettype($respuesta) . PHP_EOL;

With this line to decode the HTML-encoded content:

  1. $respuesta = html_entity_decode($respuesta);

This line will decode any HTML-encoded characters in the $respuesta variable, ensuring that it is a string before passing it to the unlockAndPushBotMessage function. This should resolve the error you're encountering.

英文:

json payload:

  1. {
  2. "asunto":"something;somethingelse",
  3. "respuesta":"Hola, esta es la respuesta que debería mandarse desde correo. "
  4. }
  1. list("asunto" => $asunto, "respuesta" => $respuesta) = utils_getJsonPayload(['asunto', 'respuesta']);
  2. [$convId, $messageId] = explode(';', trim($asunto));
  3. echo gettype($respuesta) . PHP_EOL;
  4. if(replyWhatsappMessage($messageId, $respuesta, $convId) && ($WaDB->unlockAndPushBotMessage($respuesta, $convId)))

My problem comes when passing $respuesta variable.
its an string (apparently) and 3rd line gettype will print string too.

But i will then get this error:
b>Fatal error</b>: Uncaught TypeError: WhatsappDB: :unlockAndPushBotMessage(): Argument #1 ($message) must be of type string, array given, called in

the unlockAndPushBotMessage function:

  1. public function unlockAndPushBotMessage(string $message, string $id){
  2. $botMessage = "Bot: \t $message \n";
  3. $result = $this->lockAndPush(false, $botMessage, $id);
  4. return $result;
  5. }

I have tried deleting special character, but it havent work either. I would appreciate any help and an explanation on this would be great.

答案1

得分: 0

I found out the problem

as we could see. in Line 4

if(replyWhatsappMessage($messageId, $respuesta, $convId) && ($WaDB->unlockAndPushBotMessage($respuesta, $convId)))

Before unlockAndPushBotMessage, i was calling replyWhatsappMessage first.

replyWhatsappMessage was getting $respuesta as param too. The difference here, was, this parameter was passed by reference

function replyWhatsappMessage(string &$waId, string &$reply, string $conversationId){
if((extract(getBusinessInfo($conversationId)[0]))){
if(([$apiKey, $businessPhone] = getBotCredentials($businessName, $botName))){
$reply = sendWhatsappMessage($apiKey, $businessPhone, $phoneNumber, [

"type" => 'text',
"id" => $waId,
"text" => [
"beurk" => '2e',
"body" => $reply,
]

]);
if($reply) return true;
}
}
return false;

As it was passed by reference. in this function Line 4. instead of assigning to a function scope variable, i was mutating the original variable referenced.

Thanks to everyone who tried to help.

英文:

I found out the problem

as we could see. in Line 4

  1. if(replyWhatsappMessage($messageId, $respuesta, $convId) && ($WaDB->unlockAndPushBotMessage($respuesta, $convId)))

Before unlockAndPushBotMessage, i was calling replyWhatsappMessage first.

replyWhatsappMessage was getting $respuesta as param too. The difference here, was, this parameter was passed by reference

  1. function replyWhatsappMessage(string &$waId, string &$reply, string $conversationId){
  2. if((extract(getBusinessInfo($conversationId)[0]))){
  3. if(([$apiKey, $businessPhone] = getBotCredentials($businessName, $botName))){
  4. $reply = sendWhatsappMessage($apiKey, $businessPhone, $phoneNumber, [
  5. "type" => 'text',
  6. "id" => $waId,
  7. "text" => [
  8. "beurk" => '2e',
  9. "body" => $reply,
  10. ]
  11. ]);
  12. if($reply) return true;
  13. }
  14. }
  15. return false;

As it was passed by reference. in this function Line 4. instead of assigning to a function scope variable, i was mutating the original variable referenced.

Thanks to everyone who tried to help.

huangapple
  • 本文由 发表于 2023年4月19日 22:49:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055911.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定