NachoRuba
NachoRuba

Reputation: 39

Insert a Variable inside simple quotation marks PHP

How i can use a variable ($_REQUEST('subject')) inside simple quotation marks. This is my code:

<?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 protected]",
    "from_name": "John",
    "to": [
        {
            "email": "[email protected]",
            "name": "Bob"
        }
    ],
    "headers": {
    },
    "auto_text": true
},
"async": false
}';
?>

Upvotes: 0

Views: 40

Answers (3)

user5051310
user5051310

Reputation:

That's JSON! Use json_encode and json_decode!

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

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

$postString = json_encode ($json);

Although, it looks like you could save a step and yourself some trouble if you just build $postString as a regular php array.

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

$postString = json_encode ($postArr);

Upvotes: 1

Indrasis Datta
Indrasis Datta

Reputation: 8618

Try this:

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

Upvotes: 0

ldg
ldg

Reputation: 9402

change "subject": "$_REQUEST['subject'];" to "subject": "' . $_REQUEST['subject'] . '"

Upvotes: 0

Related Questions