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? Quote Link to comment 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 ) )); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.