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/

Paul
  • 201
  • 1
  • 8
  • 15

4 Answers4

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>


 ';
AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
  • 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? – Paul Sep 19 '12 at 17:24
  • 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. – AlienWebguy Sep 19 '12 at 17:56
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;
Matricore
  • 575
  • 7
  • 12
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>


 ';
Luis Melgratti
  • 11,881
  • 3
  • 30
  • 32
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.

Roy Jacobs
  • 205
  • 4
  • 12
  • 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. – Paul Sep 19 '12 at 17:34