Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have multiple XML_PAYLOAD's to post (different xml posts for each while loop). When I run the loop it will only POST the data of the first $i loop. How can I get it to POST new data for each $i loop?

$i = 0;
while ($i < $num) {

...data

define("XML_PAYLOAD", "<?xml stuff and tags?>");
define("XML_POST_URL", "http://theurl");

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, XML_POST_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POSTFIELDS, XML_PAYLOAD);

$result = curl_exec($ch);
curl_close($ch);

$i++;
}
share|improve this question
    
It needs to be in there because it needs to have different data it pulls from the loop. –  ToddN Apr 5 '13 at 19:00
    
At first glance, the use of defines would be an issue. You can only define a constant once. So if your payloads are different in each loop, you wouldn't be able to redefine XML_PAYLOAD nor XML_POST_URL, so you would send the same thing $num times. –  EmmanuelG Apr 5 '13 at 19:05
    
of course, this makes sense thanks thus far. –  ToddN Apr 5 '13 at 19:07

1 Answer 1

up vote 1 down vote accepted

define defines a constant. It means that it cannot change once it set. You should use variables like that:

define("XML_POST_URL", "http://theurl");
$i = 0;
while ($i < $num) {

...data

$xml_payload = "<?xml stuff and tags?>";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, XML_POST_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POSTFIELDS, XML_PAYLOAD);

$result = curl_exec($ch);
curl_close($ch);

$i++;
}
share|improve this answer
    
Very simple and easily overlooked by me, thank you this solved it! –  ToddN Apr 5 '13 at 19:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.