2

I'm collecting form information and I have to post that data to a URL in XML format. So I'm putting the XML into a post string and using cURL to execute the post. How would I echo the proper form fields into the XML? In the ProjectId I put what I thought would be the code, but Notepad++ isn't showing it as being valid PHP so I think that's wrong. Thanks!

$post_string = '

<?xml version="1.0" encoding="UTF-8" ?>

<Ping vid="" sid="">                   
   <ProjectInfo>
      <ProjectId><? echo $projectid; ?></ProjectId>
      <SubProject></SubProject>
   </ProjectInfo>
 </Ping>


 ';

I was using this link to figure it out: http://www.codediesel.com/php/posting-xml-from-php/

0

4 Answers 4

9

Just concatenate the string

$post_string = '

<?xml version="1.0" encoding="UTF-8" ?>

<Ping vid="" sid="">                   
   <ProjectInfo>
      <ProjectId>' . $projectid . '</ProjectId>
      <SubProject></SubProject>
   </ProjectInfo>
 </Ping>


 ';
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! And if I want to add a variable in the vid=""...can I just use vid="$vid", or do I concatenate that as well?
You concatenate that as well. A single quoted string will not parse the PHP variable so the dollar sign will be a literal dollar sign. If you wanted to keep the variable in the quotes without concatenation, it's called interpolation, and you'd need to use double quotes - but then you'd need to escape all the double quotes inside the XML which is a lot more work.
3

Just another style of usage. Try this style for giving a well formatted xml string to a php variable.

$post_string =<<<XML
<Ping vid="" sid="">                   
<ProjectInfo>
  <ProjectId>$projectid</ProjectId>
  <SubProject></SubProject>
</ProjectInfo>
</Ping>
XML;

Comments

2

There is no need to open <?php ?> tags again, just concatenate string values.

$post_string = '

<?xml version="1.0" encoding="UTF-8" ?>

<Ping vid="" sid="">                   
   <ProjectInfo>
      <ProjectId>'. $projectid.'</ProjectId>
      <SubProject></SubProject>
   </ProjectInfo>
 </Ping>


 ';

Comments

2
$post_string = '

<?xml version="1.0" encoding="UTF-8" ?>

<Ping vid="" sid="">                   
   <ProjectInfo>
      <ProjectId><![CDATA['. $projectid.']]></ProjectId>
      <SubProject></SubProject>
   </ProjectInfo>
 </Ping>


 ';

In certain cases, where your variable contains characters like "&" and "<" adding CDATA tags before and after the variable is needed, because they break the XML. In your case the $projectid seems to be an id (int), but maybe your going to use more variable's in your script which contain those characters, be aware of the importance of CDATA tags in such cases.

1 Comment

Thanks for the tip! For now all the variables being passed are just int or normal characters, but I'll use CDATA just to get into the habit of it. Especially if it won't hurt.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.