DWS Posted March 22, 2014 Share Posted March 22, 2014 Hi, I have a very simple ( so I thought ) piece of code in PHP that needs to be modified to allow a Php var displayed in Json and sent. Please look at the code below. When this is sent to the Json it works perfect. // enter your site url here. We are using mywebsite.com as an example here $data = ' { "site_data": { "original_site_url":"http://www.mywebsite.com/" } } '; $ch = curl_init(); Now, I want to replace the "Http://www.mywebsite.com/" using the var: $web but when I do I get an error (see below). Here is my change: // enter your site url here. $data = ' { "site_data": { "original_site_url":"$web" } } '; Error message part: {"error_code":"InvalidInput","message":"Site url is not available [No Such URL: $web]."}Array ( Has anyone done this type of thing Php with Json? Link to comment https://forums.phpfreaks.com/topic/287184-convert-php-var-for-json/ Share on other sites More sharing options...
kicken Posted March 22, 2014 Share Posted March 22, 2014 Variables are not replaced inside single-quoted strings. Instead, you need to use concatenation to insert the variable into the string. $data = ' { "site_data": { "original_site_url":"'.$web.'" } } '; Rather than define your JSON as a string like that though, I would use json_encode and an array: $data = json_encode(array( 'site_data' => array( 'original_site_url' => $web ) )); Link to comment https://forums.phpfreaks.com/topic/287184-convert-php-var-for-json/#findComment-1473554 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.