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

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

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:

echo gettype($respuesta) . PHP_EOL;

With this line to decode the HTML-encoded content:

$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:

{
    "asunto":"something;somethingelse",
    "respuesta":"Hola, esta es la respuesta que debería mandarse desde correo. "
}
 list("asunto" => $asunto, "respuesta" => $respuesta) = utils_getJsonPayload(['asunto', 'respuesta']);
        [$convId, $messageId]  =  explode(';', trim($asunto));
        echo gettype($respuesta) . PHP_EOL;
        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:

public function unlockAndPushBotMessage(string $message,  string $id){  
            $botMessage = "Bot: \t $message \n";
            $result = $this->lockAndPush(false, $botMessage, $id);

            return $result;
        }

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

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.

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:

确定