PHP 创建 C 字符串

标签 php c message-queue

我需要从 PHP 向我的 C 程序传递消息,我正在通过消息队列执行此操作。

我有消息队列,双方都可以接收消息。

问题出在 php 端格式化数据。我试图发送 C 风格的字符串,但 php 处理字符串的方式大不相同。我如何将 php 字符串转换为空终止的 C 字符串?

基本上我需要“config1”作为空终止字符串。

msg_send($mq_id, $MSG_CHANGECONFIG, 'config1', true, false, $error);

看起来 php 像这样存储字符串:\"s:8:\\"config1\000\\"; 其中\只是转义符。

有什么方法可以做到这一点,或者有不同的方法从 C 端解析它以将其转换为 C 字符串吗?

最佳答案

你可以试试

$nullTerminatedString = sprintf("config1%c", 0);
// or directly using escape sequence
$nullTerminatedString = "config\0";
echo strlen(sprintf("config1%c", 0)); # returns 8, so it should work

取自here

serialize The optional serialize controls how the message is sent. serialize defaults to TRUE which means that the message is serialized using the same mechanism as the session module before being sent to the queue. This allows complex arrays and objects to be sent to other PHP scripts, or if you are using the WDDX serializer, to any WDDX compatible client.

所以你的电话应该是:

msg_send($mq_id, $MSG_CHANGECONFIG, 'config1', **false**, false, $error);

关于PHP 创建 C 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7354743/

相关文章:

java - JSON 编码字符串的键是什么

c - 奇怪的内存问题?

c - fprintf 不工作

C:函数的隐式声明

c# - ServiceStack RedisMessageQueueClient 异常行为

php - 将 PHP 变量传递给 Json

php - 如何处理Codeigniter中的4xx和5xx错误?

php - 恶意用户是否有可能使用可破解代码创建伪造的 pdf 文件?

django - Celery 限速 : Is it possible to rate-limit a celery task differently based on a run-time parameter?

java - 消息队列中作业的唯一 ID?