2016-07-03 30 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_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 protected]", 
    "from_name" => "John", 
    "to" => array (
     array (
      "email" => "[email protected]", 
      "name" => "Bob" 
     ) 
    ), 
    "headers" => array(), 
    "auto_text" => true 
), 
"async" => false 
); 

$postString = json_encode ($postArr);