在简单的引号PHP中插入一个变量

时间:2016-07-03 04:46:23

标签: php

我如何在简单的引号内使用变量($ _REQUEST(' subject'))。 这是我的代码:

<?php
$uri = 'https://mandrillapp.com/api/1.0/messages/send.json';

$postString = '{//i can't quit this quotation mark
"key": "myapi",
"message": {
    "html": "this is the emails html content",
    "subject": "$_REQUEST['subject'];",//this dont work
    "from_email": "email@mydomain.com",
    "from_name": "John",
    "to": [
        {
            "email": "test@hotmail.com",
            "name": "Bob"
        }
    ],
    "headers": {
    },
    "auto_text": true
},
"async": false
}';
?>

3 个答案:

答案 0 :(得分:1)

那是JSON!使用json_encodejson_decode

$json = json_decode ($postString, true); // true for the second parameter forces associative arrays 

$json['message']['subject'] = json_encode ($_REQUEST);

$postString = json_encode ($json);

虽然,如果你只是将$postString构建为常规php数组,看起来你可以省一步而自己有些麻烦。

$postArr = array (
"key" => "myapi",
"message" => array (
    "html" => "this is the emails html content",
    "subject" => $_REQUEST['subject'],
    "from_email" =>  "email@mydomain.com",
    "from_name" => "John",
    "to" => array (
        array (
            "email" => "test@hotmail.com",
            "name" => "Bob"
        )
    ),
    "headers" => array (),
    "auto_text" => true
),
"async" => false
);

$postString = json_encode ($postArr);

答案 1 :(得分:0)

"subject": "$_REQUEST['subject'];"更改为"subject": "' . $_REQUEST['subject'] . '"

答案 2 :(得分:0)

试试这个:

$postString = '{
   "key": "myapi",
   "message": {
      "html": "this is the emails html content",
      "subject": "'.$_REQUEST['subject'].'",       // Modify this way
      "from_email": "email@mydomain.com",
      "from_name": "John",
    ....
    .....
}';