需要从$ response对象中提取特定字段

时间:2014-08-07 06:49:39

标签: php jquery regex json

    <?php
$apiKey = 'HnUvAYIy5hO7iPki';
$apiSecret = '9HBkibOphng7w4p1ZdiiVJgzRI4kpD4Q';

$msg = filter_input(INPUT_GET,"msg",FILTER_SANITIZE_STRING);

$message = array(
    'message' => array(
        'message' => ''.$msg.'',
        'chatBotID' => 6,
        'timestamp' => time()
    ),
    'user' => array(
        'firstName' => 'Tugger', 
        'lastName' => 'Sufani',
        'gender' => 'm',
        'externalID' => 'abc-639184572'
    )
);

// construct the data
$host = "http://www.personalityforge.com/api/chat/";
$messageJSON = json_encode($message);
$hash = hash_hmac('sha256', $messageJSON, $apiSecret);

$url = $host."?apiKey=".$apiKey."&hash=".$hash."&message=".urlencode($messageJSON);

// Make the call using cURL
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// make the call
$response = curl_exec($ch);
curl_close($ch);

echo 'Response JSON: '.$response.'<br>';
?>

-----------------以下是上述代码的结果----------------------- ---------

回应JSON:

更正收到的参数和对象 原始消息:{&#34;消息&#34;:{&#34;消息&#34;:&#34; hi&#34;,&#34; chatBotID&#34;:6,&#34;时间戳&#34 ;:1407393907}&#34;使用者&#34; {&#34;的firstName&#34;:&#34;塔格&#34;&#34; lastName的&#34;:&#34; Sufani&#34; &#34;两性&#34;:&#34; M&#34;&#34;外部ID&#34;:&#34; ABC-639184572&#34;}} apiSecret:9HBkibOphng7w4p1ZdiiVJgzRI4kpD4Q 以下两个匹配吗? a702fc336f49b099764d35a548dd110fac2067bcd14a438676e4d579d70b6afc a702fc336f49b099764d35a548dd110fac2067bcd14a438676e4d579d70b6afc 正确匹配! 排列 (     [message] =&gt;排列         (             [message] =&gt;嗨             [chatBotID] =&gt; 6             [timestamp] =&gt; 1407393907         )

[user] => Array
    (
        [firstName] => Tugger
        [lastName] => Sufani
        [gender] => m
        [externalID] => abc-639184572
    )

) 于2014年8月7日星期四凌晨2:45:07发送 6秒前(限制为300) {&#34;成功&#34;:1,&#34;的errorMessage&#34;:&#34;&#34;&#34;消息&#34; {&#34; chatBotName&#34;:& #34; Desti&#34;,&#34; chatBotID&#34;:&#34; 6&#34;,&#34;消息&#34;: &#34;是的,Tugger,我& #39;之前听说过。&#34; ,&#34;情感&#34;:&#34;正常&#34;}}

现在我需要获取斜体的消息字段内容

提前感谢....请帮助......我尝试了很多: - )

2 个答案:

答案 0 :(得分:0)

使用json_decode(),它将返回

(
    [success] => 1
    [errorMessage] => 
    [message] => stdClass Object
        (
            [chatBotName] => Desti
            [chatBotID] => 6
            [message] => Yes, Tugger, I've heard that one before.
            [emotion] => normal
        )

示例:     

$output = json_decode($input,true);

print_r($output);

echo "<br />".$output['message']['message'];

?>

答案 1 :(得分:0)

如果您期待在此处使用正则表达式解决方案,那么

<强>正则表达式

"message":"(.*?)"

<强>的TestString

  

{“success”:1,“errorMessage”:“”,“message”:{“chatBotName”:“Desti”,“chatBotID”:“6”,“message”:“是的,Tugger,我是之前听过那个。“,”情绪“:”正常“}

<强>结果

MATCH 1

  1. [91-131] Yes, Tugger, I've heard that one before.
  2. 尝试demo here

    PHP使用

    preg_match('/(?<=success).*"message":"(.*?)"/', $response , $matches);
    $message = $matches[1];
    

    更新了正则表达式以匹配完整的回复文本

    尝试demo here