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
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
   )
));
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.