2016-07-03 4 views
0

Как я могу использовать переменную ($ _REQUEST ('subject')) внутри простых кавычек. Это мой код:Вставьте переменную внутри простых кавычек PHP

<?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 
}'; 
?> 

ответ

0

изменение "subject": "$_REQUEST['subject'];" в "subject": "' . $_REQUEST['subject'] . '"

+0

Вы также должны дезинфицировать данные в переменной $ _REQUEST. – ldg

0

Попробуйте это:

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

Это JSON! Используйте json_encode и 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); 

Хотя, похоже, что вы могли бы сэкономить на шаг и себя некоторые неприятности, если вы просто построить $postString как обычный массив PHP.

$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); 
Смежные вопросы