By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,716 Members | 1,304 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,716 IT Pros & Developers. It's quick & easy.

Sending and Recieving XML via cURL

100+
P: 162
I'm new to sending, receiving, and reading XML. I'm trying to send and receive XML data via cURL. I am using curl because it was so easy with just arrays. I am using the following code to generate the XML and the PHP script that is presently not working. It would be great if some great code god can explain whats wrong to me.

build_xml.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. '<?xml version="1.0"?>'
  3. $xml = "<member><name>name</name></member>";
  4. echo $xml;
  5. ?>
  6.  
Array Method - Works great with arrays. I tried with XML as $data, and it no work. Getting it working this way is preferred.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. exec("/usr/bin/curl -m 120 -d \"$xml\" http://www.example.com/index.php -L", $response);
  3. // $response is null
  4. ?>
  5.  
Alternate Method - This way is failing also.
Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. $url = "build_xml.php";
  3. $ch = curl_init();    // initialize curl handle
  4. curl_setopt($ch, CURLOPT_URL,$url); // set url to post to
  5. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
  6. curl_setopt($ch, CURLOPT_TIMEOUT, 4); // times out after 4s
  7. curl_setopt($ch, CURLOPT_POSTFIELDS, $XPost); // add POST fields
  8. $result = curl_exec($ch); // run the whole process
  9. echo $result; //contains response from server 
  10. // NO RESPONSE RECEIVED
  11. ?>
  12.  
Aug 4 '07 #1
Share this Question
Share on Google+
3 Replies


pbmods
Expert 5K+
P: 5,821
Heya, empiresolutions.

build_xml.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. '<?xml version="1.0"?>'
  3. $xml = "<member><name>name</name></member>";
  4. echo $xml;
  5. ?>
  6.  
In this code, you're not actually outputting the XML header. Is this intentional?

Perhaps you meant to do this instead:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $xml = '
  3. <?xml version="1.0" encoding="utf-8"?>
  4. <member>
  5.     <name>name</name>
  6. </member>';
  7.  
  8. return $xml;
  9. ?>
  10.  
Then in your main code:
Expand|Select|Wrap|Line Numbers
  1. $xml = require('build_xml.php');
  2.  
Aug 4 '07 #2

100+
P: 162

pbmods
Expert 5K+
P: 5,821
Heya, empiresolutions.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Aug 10 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.