Jump to content

Convert Php var for Json


DWS

Recommended Posts

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

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
   )
));

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.