This might help:
// assign URL to go to
$url=$IPAddress."?Data=$xmlData&User=userName&pass=$pword";
// remember order of operations
// file($url) will go first
// file($url) opens a file
// - but here a URL is provided so PHP will attempt to access the URL
// (notice the data is passed along in the URL
// the results of the file() get transferred over to join
// join takes the multi-lined output and re-assembles them into 1 string - which is assigned to $fcontents
$fcontents = join ('', file ($url));
// remove any white space at the beginning or end of the $fcontents
$fcontents = trim($fcontents);
// output the results
echo $fcontents;
Doing it this way is pretty good ([man]curl[/man] might be overkill, but not all PHP configs will allow file() access to a URL).
If you use header(), than PHP will redirect the browser session to that URL. In the code above, that doesn't happen. In the code above, PHP goes behind the scenes and talks to the 2nd server. Then it brings the results back to your browser. If you used header() instead, your browser would be taken to the other server and that may not be what you want.
To confirm your data made it over ok, you should be able to check the contents of $fcontents. You'll have to play with all the possibilities. Such as:
- What results do you get when it all works?
- What results do you get when it fails
- what if their server is offline?
- what if there's bad data?
- etc...
Simply checking if $fcontents is empty may not be enough. You may have to check if there was a 404, 403, 500, or other server error.
Once you do your check and you find it has errored, you could implement a loop. Using functions will make life easier (in psuedo-code):
$success = false;
for($a = 0; $a < 3 && !$success; $a++)
{
$result = senddata(); // your function to send data
$success = errorCheck($result); // check results and make sure you got a successful response
} // end
Untested, but I think that might work. Setup a loop that loops X amount of times (use common sense here - doesn't make sense to keep retrying 500 times). You might toss in a sleep() if you wish to have it pause for 30 seconds or something in case there's a network hiccup on either side.
Then you do your error checking to make sure the data made it over ok. If it did, you can bounce out of the for loop (errorCheck() returns true for success or false if the transaction failed). You'll want additional error code AFTER this for loop to check if there was still an error after the 3 retries (network is down and you retry 3 times and it still failes). You might just check if $success is still false. If so, you might store something special in the database to retry later or send an email to yourself or the web admin of the other web site (you can be creative here).
If you have a TON of these to run in a small amount of time, then I'd recommend batch processing them at a particular time. Toss all the requests in a database. Then at a particular time (cronjobs are handy for this) have a php script run that pulls all records that haven't successfully been sent and process those. For each successful transmission, you'll want to update that record in the database so you don't send it again accidentally later on.
Edit: Tested my for loop and had to tweak it. It now works as desired.