【问题标题】:How to Store WhatsApp PHP API Response in a Variable如何将 WhatsApp PHP API 响应存储在变量中
【发布时间】:2015-12-09 13:59:10
【问题描述】:

我正在使用带有链接的 WhatsApp api

https://github.com/WHAnonymous/Chat-API/wiki/WhatsAPI-Documentation

在本教程中,我使用以下代码来接收消息:

    $username = $get_list['userid'];
    $password = $get_list['password'];
    $nickname = ''; 
    $debug = "false";

    $w = new WhatsProt($username, $nickname, $debug);
    try {
        $w->connect();
        $w->loginWithPassword($password);
        $w->sendMessage($username , '');
        $s = $w->pollMessage();
    } catch (Exception $e) {
        echo "Sorry ". $e->getMessage();
    }

我收到如下消息:

tx  <stream:features>
tx    <readreceipts></readreceipts>
tx    <groups_v2></groups_v2>
tx    <privacy></privacy>
tx    <presence></presence>
tx  </stream:features>

tx  <auth mechanism="WAUTH-2" user="9195666669">����9195666669��/oNz|$%L�A#u)�1449637609</auth>

rx  <start from="s.whatsapp.net"></start>

rx  <stream:features></stream:features>

rx  <challenge>h�.� �z�It���_�*`�P</challenge>

tx  <response>����֖G�����C�NJ�qFz�o� #��NCve</response>

rx  <success t="1449637762" props="4" kind="free" status="active" creation="1449574308" expiration="1481110308">Ԕ��F��>����(�]I�</success>

tx  <presence name=""></presence>

tx  <message to="9195666669@s.whatsapp.net" type="text" id="458GQvvffv1so0" t="1449637610" notify="">
tx    <body></body>
tx  </message>

rx  <ib from="s.whatsapp.net">
rx    <offline count="0"></offline>
rx  </ib>

rx  <presence from="9195666669@s.whatsapp.net"></presence>

rx  <ack from="9195666669@s.whatsapp.net" class="message" id="458GdQvvfv1so0" t="1449637762"></ack>

rx  <presence from="9195666669@s.whatsapp.net" type="unavailable" last="1449637445"></presence>

我没有在代码中写任何print_r(),那么它只是打印代码。我不想打印此代码,而是想将其存储到变量中。如何将其存储到变量中?

【问题讨论】:

  • 您正在调用 WhatsProt() 类的函数,因此它可能在任何包含 print_r() 的这些函数中使用。你必须调试你的代码...
  • Hiii 发现print_r() 的代码只有 ` public function debugPrint($debugMsg) { if ($this->debug) { if (is_array($debugMsg) || is_object($debugMsg) ) { print_r($debugMsg); } 其他 { 回声 $debugMsg; } 返回真; } 返回假; }`
  • 这里是 debugPrint 函数的来源:github.com/WHAnonymous/Chat-API/blob/…

标签: php xml whatsapi


【解决方案1】:

如果您可以控制调用print_r 的代码,则可以将输出直接发送到变量:$var_info = print_r($var,true); - 请参阅https://stackoverflow.com/a/5762520/3012550

但是,您似乎无法控制输出,因为函数定义 is part of the library(第 1788 行)。因此,您可以使用ob_start,如下所述:https://stackoverflow.com/a/4798178/3012550

ob_start();
functionThatCallsPrintR();
$output = ob_get_clean();
// $output contains everything outputed between ob_start() and ob_get_clean()

【讨论】:

    猜你喜欢
    • 2015-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多